]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
ITS#8722 fix FIRST_DUP/LAST_DUP cursor bounds check
[openldap] / libraries / liblmdb / mdb.c
index 36d50b82bbb587fd07cfbef7a9d00aad78597a59..b47cb53a2ce1082c900437ec853db4c1c58b2b31 100644 (file)
@@ -5,7 +5,7 @@
  *     BerkeleyDB API, but much simplified.
  */
 /*
- * Copyright 2011-2016 Howard Chu, Symas Corp.
+ * Copyright 2011-2017 Howard Chu, Symas Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -41,6 +41,8 @@
 #ifdef _WIN32
 #include <malloc.h>
 #include <windows.h>
+#include <wchar.h>                             /* get wcscpy() */
+
 /** getpid() returns int; MinGW defines pid_t but MinGW64 typedefs it
  *  as int64 which is wrong. MSVC doesn't define it at all, so just
  *  don't use it.
@@ -111,6 +113,10 @@ typedef SSIZE_T    ssize_t;
 /* Most platforms have posix_memalign, older may only have memalign */
 #define HAVE_MEMALIGN  1
 #include <malloc.h>
+/* On Solaris, we need the POSIX sigwait function */
+#if defined (__sun)
+# define _POSIX_PTHREAD_SEMANTICS      1
+#endif
 #endif
 
 #if !(defined(BYTE_ORDER) || defined(__BYTE_ORDER))
@@ -118,7 +124,7 @@ typedef SSIZE_T     ssize_t;
 #include <resolv.h>    /* defines BYTE_ORDER on HPUX and Solaris */
 #endif
 
-#if defined(__APPLE__) || defined (BSD)
+#if defined(__APPLE__) || defined (BSD) || defined(__FreeBSD_kernel__)
 # define MDB_USE_POSIX_SEM     1
 # define MDB_FDATASYNC         fsync
 #elif defined(ANDROID)
@@ -127,6 +133,7 @@ typedef SSIZE_T     ssize_t;
 
 #ifndef _WIN32
 #include <pthread.h>
+#include <signal.h>
 #ifdef MDB_USE_POSIX_SEM
 # define MDB_USE_HASH          1
 #include <semaphore.h>
@@ -338,15 +345,15 @@ mdb_sem_wait(sem_t *sem)
 }
 
 #else  /* MDB_USE_POSIX_MUTEX: */
-       /** Shared mutex/semaphore as it is stored (mdb_mutex_t), and as
-        *      local variables keep it (mdb_mutexref_t).
+       /** Shared mutex/semaphore as the original is stored.
         *
-        *      When #mdb_mutexref_t is a pointer declaration and #mdb_mutex_t is
-        *      not, then it is array[size 1] so it can be assigned to a pointer.
-        *      @{
+        *      Not for copies.  Instead it can be assigned to an #mdb_mutexref_t.
+        *      When mdb_mutexref_t is a pointer and mdb_mutex_t is not, then it
+        *      is array[size 1] so it can be assigned to the pointer.
         */
-typedef pthread_mutex_t mdb_mutex_t[1], *mdb_mutexref_t;
-       /*      @} */
+typedef pthread_mutex_t mdb_mutex_t[1];
+       /** Reference to an #mdb_mutex_t */
+typedef pthread_mutex_t *mdb_mutexref_t;
        /** Lock the reader or writer mutex.
         *      Returns 0 or a code to give #mdb_mutex_failed(), as in #LOCK_MUTEX().
         */
@@ -871,19 +878,26 @@ typedef struct MDB_page {
        /** Header for a single key/data pair within a page.
         * Used in pages of type #P_BRANCH and #P_LEAF without #P_LEAF2.
         * We guarantee 2-byte alignment for 'MDB_node's.
+        *
+        * #mn_lo and #mn_hi are used for data size on leaf nodes, and for child
+        * pgno on branch nodes.  On 64 bit platforms, #mn_flags is also used
+        * for pgno.  (Branch nodes have no flags).  Lo and hi are in host byte
+        * order in case some accesses can be optimized to 32-bit word access.
+        *
+        * Leaf node flags describe node contents.  #F_BIGDATA says the node's
+        * data part is the page number of an overflow page with actual data.
+        * #F_DUPDATA and #F_SUBDATA can be combined giving duplicate data in
+        * a sub-page/sub-database, and named databases (just #F_SUBDATA).
         */
 typedef struct MDB_node {
-       /** lo and hi are used for data size on leaf nodes and for
-        * child pgno on branch nodes. On 64 bit platforms, flags
-        * is also used for pgno. (Branch nodes have no flags).
-        * They are in host byte order in case that lets some
-        * accesses be optimized into a 32-bit word access.
-        */
+       /** part of data size or pgno
+        *      @{ */
 #if BYTE_ORDER == LITTLE_ENDIAN
-       unsigned short  mn_lo, mn_hi;   /**< part of data size or pgno */
+       unsigned short  mn_lo, mn_hi;
 #else
        unsigned short  mn_hi, mn_lo;
 #endif
+       /** @} */
 /** @defgroup mdb_node Node Flags
  *     @ingroup internal
  *     Flags for node headers.
@@ -1237,7 +1251,7 @@ typedef struct MDB_pgstate {
 struct MDB_env {
        HANDLE          me_fd;          /**< The main data file */
        HANDLE          me_lfd;         /**< The lock file */
-       HANDLE          me_mfd;                 /**< just for writing the meta pages */
+       HANDLE          me_mfd;         /**< For writing and syncing the meta pages */
        /** Failed to update the meta page. Probably an I/O error. */
 #define        MDB_FATAL_ERROR 0x80000000U
        /** Some fields are initialized. */
@@ -1415,7 +1429,8 @@ static SECURITY_DESCRIPTOR mdb_null_sd;
 static SECURITY_ATTRIBUTES mdb_all_sa;
 static int mdb_sec_inited;
 
-static int utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize);
+struct MDB_name;
+static int utf8_to_utf16(const char *src, struct MDB_name *dst, int xtra);
 #endif
 
 /** Return the library version info. */
@@ -1744,6 +1759,7 @@ mdb_dcmp(MDB_txn *txn, MDB_dbi dbi, const MDB_val *a, const MDB_val *b)
 
 /** Allocate memory for a page.
  * Re-use old malloc'd pages first for singletons, otherwise just malloc.
+ * Set #MDB_TXN_ERROR on failure.
  */
 static MDB_page *
 mdb_page_malloc(MDB_txn *txn, unsigned num)
@@ -2111,7 +2127,7 @@ mdb_page_dirty(MDB_txn *txn, MDB_page *mp)
 }
 
 /** Allocate page numbers and memory for writing.  Maintain me_pglast,
- * me_pghead and mt_next_pgno.
+ * me_pghead and mt_next_pgno.  Set #MDB_TXN_ERROR on failure.
  *
  * If there are free pages available from older transactions, they
  * are re-used first. Otherwise allocate a new page at mt_next_pgno.
@@ -2234,7 +2250,7 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
                np = m2.mc_pg[m2.mc_top];
                leaf = NODEPTR(np, m2.mc_ki[m2.mc_top]);
                if ((rc = mdb_node_read(&m2, leaf, &data)) != MDB_SUCCESS)
-                       return rc;
+                       goto fail;
 
                idl = (MDB_ID *) data.mv_data;
                i = idl[0];
@@ -2384,6 +2400,7 @@ mdb_page_unspill(MDB_txn *txn, MDB_page *mp, MDB_page **ret)
 }
 
 /** Touch a page: make it dirty and re-insert into tree with updated pgno.
+ * Set #MDB_TXN_ERROR on failure.
  * @param[in] mc cursor pointing to the page to be touched
  * @return 0 on success, non-zero on failure.
  */
@@ -3824,7 +3841,10 @@ mdb_env_write_meta(MDB_txn *txn)
        len = sizeof(MDB_meta) - off;
        off += (char *)mp - env->me_map;
 
-       /* Write to the SYNC fd */
+       /* Write to the SYNC fd unless MDB_NOSYNC/MDB_NOMETASYNC.
+        * (me_mfd goes to the same file as me_fd, but writing to it
+        * also syncs to disk.  Avoids a separate fdatasync() call.)
+        */
        mfd = (flags & (MDB_NOSYNC|MDB_NOMETASYNC)) ? env->me_fd : env->me_mfd;
 #ifdef _WIN32
        {
@@ -4079,6 +4099,189 @@ mdb_fsize(HANDLE fd, size_t *size)
        return MDB_SUCCESS;
 }
 
+
+#ifdef _WIN32
+typedef wchar_t        mdb_nchar_t;
+# define MDB_NAME(str) L##str
+# define mdb_name_cpy  wcscpy
+#else
+/** Character type for file names: char on Unix, wchar_t on Windows */
+typedef char   mdb_nchar_t;
+# define MDB_NAME(str) str             /**< #mdb_nchar_t[] string literal */
+# define mdb_name_cpy  strcpy  /**< Copy name (#mdb_nchar_t string) */
+#endif
+
+/** Filename - string of #mdb_nchar_t[] */
+typedef struct MDB_name {
+       int mn_len;                                     /**< Length  */
+       int mn_alloced;                         /**< True if #mn_val was malloced */
+       mdb_nchar_t     *mn_val;                /**< Contents */
+} MDB_name;
+
+/** Filename suffixes [datafile,lockfile][without,with MDB_NOSUBDIR] */
+static const mdb_nchar_t *const mdb_suffixes[2][2] = {
+       { MDB_NAME("/data.mdb"), MDB_NAME("")      },
+       { MDB_NAME("/lock.mdb"), MDB_NAME("-lock") }
+};
+
+#define MDB_SUFFLEN 9  /**< Max string length in #mdb_suffixes[] */
+
+/** Set up filename + scratch area for filename suffix, for opening files.
+ * It should be freed with #mdb_fname_destroy().
+ * On Windows, paths are converted from char *UTF-8 to wchar_t *UTF-16.
+ *
+ * @param[in] path Pathname for #mdb_env_open().
+ * @param[in] envflags Whether a subdir and/or lockfile will be used.
+ * @param[out] fname Resulting filename, with room for a suffix if necessary.
+ */
+static int ESECT
+mdb_fname_init(const char *path, unsigned envflags, MDB_name *fname)
+{
+       int no_suffix = F_ISSET(envflags, MDB_NOSUBDIR|MDB_NOLOCK);
+       fname->mn_alloced = 0;
+#ifdef _WIN32
+       return utf8_to_utf16(path, fname, no_suffix ? 0 : MDB_SUFFLEN);
+#else
+       fname->mn_len = strlen(path);
+       if (no_suffix)
+               fname->mn_val = (char *) path;
+       else if ((fname->mn_val = malloc(fname->mn_len + MDB_SUFFLEN+1)) != NULL) {
+               fname->mn_alloced = 1;
+               strcpy(fname->mn_val, path);
+       }
+       else
+               return ENOMEM;
+       return MDB_SUCCESS;
+#endif
+}
+
+/** Destroy \b fname from #mdb_fname_init() */
+#define mdb_fname_destroy(fname) \
+       do { if ((fname).mn_alloced) free((fname).mn_val); } while (0)
+
+#ifdef O_CLOEXEC /* POSIX.1-2008: Set FD_CLOEXEC atomically at open() */
+# define MDB_CLOEXEC           O_CLOEXEC
+#else
+# define MDB_CLOEXEC           0
+#endif
+
+/** File type, access mode etc. for #mdb_fopen() */
+enum mdb_fopen_type {
+#ifdef _WIN32
+       MDB_O_RDONLY, MDB_O_RDWR, MDB_O_META, MDB_O_COPY, MDB_O_LOCKS
+#else
+       /* A comment in mdb_fopen() explains some O_* flag choices. */
+       MDB_O_RDONLY= O_RDONLY,                            /**< for RDONLY me_fd */
+       MDB_O_RDWR  = O_RDWR  |O_CREAT,                    /**< for me_fd */
+       MDB_O_META  = O_WRONLY|MDB_DSYNC     |MDB_CLOEXEC, /**< for me_mfd */
+       MDB_O_COPY  = O_WRONLY|O_CREAT|O_EXCL|MDB_CLOEXEC, /**< for #mdb_env_copy() */
+       /** Bitmask for open() flags in enum #mdb_fopen_type.  The other bits
+        * distinguish otherwise-equal MDB_O_* constants from each other.
+        */
+       MDB_O_MASK  = MDB_O_RDWR|MDB_CLOEXEC | MDB_O_RDONLY|MDB_O_META|MDB_O_COPY,
+       MDB_O_LOCKS = MDB_O_RDWR|MDB_CLOEXEC | ((MDB_O_MASK+1) & ~MDB_O_MASK) /**< for me_lfd */
+#endif
+};
+
+/** Open an LMDB file.
+ * @param[in] env      The LMDB environment.
+ * @param[in,out] fname        Path from from #mdb_fname_init().  A suffix is
+ * appended if necessary to create the filename, without changing mn_len.
+ * @param[in] which    Determines file type, access mode, etc.
+ * @param[in] mode     The Unix permissions for the file, if we create it.
+ * @param[out] res     Resulting file handle.
+ * @return 0 on success, non-zero on failure.
+ */
+static int ESECT
+mdb_fopen(const MDB_env *env, MDB_name *fname,
+       enum mdb_fopen_type which, mdb_mode_t mode,
+       HANDLE *res)
+{
+       int rc = MDB_SUCCESS;
+       HANDLE fd;
+#ifdef _WIN32
+       DWORD acc, share, disp, attrs;
+#else
+       int flags;
+#endif
+
+       if (fname->mn_alloced)          /* modifiable copy */
+               mdb_name_cpy(fname->mn_val + fname->mn_len,
+                       mdb_suffixes[which==MDB_O_LOCKS][F_ISSET(env->me_flags, MDB_NOSUBDIR)]);
+
+       /* The directory must already exist.  Usually the file need not.
+        * MDB_O_META requires the file because we already created it using
+        * MDB_O_RDWR.  MDB_O_COPY must not overwrite an existing file.
+        *
+        * With MDB_O_COPY we do not want the OS to cache the writes, since
+        * the source data is already in the OS cache.
+        *
+        * The lockfile needs FD_CLOEXEC (close file descriptor on exec*())
+        * to avoid the flock() issues noted under Caveats in lmdb.h.
+        * Also set it for other filehandles which the user cannot get at
+        * and close himself, which he may need after fork().  I.e. all but
+        * me_fd, which programs do use via mdb_env_get_fd().
+        */
+
+#ifdef _WIN32
+       acc = GENERIC_READ|GENERIC_WRITE;
+       share = FILE_SHARE_READ|FILE_SHARE_WRITE;
+       disp = OPEN_ALWAYS;
+       attrs = FILE_ATTRIBUTE_NORMAL;
+       switch (which) {
+       case MDB_O_RDONLY:                      /* read-only datafile */
+               acc = GENERIC_READ;
+               disp = OPEN_EXISTING;
+               break;
+       case MDB_O_META:                        /* for writing metapages */
+               acc = GENERIC_WRITE;
+               disp = OPEN_EXISTING;
+               attrs = FILE_ATTRIBUTE_NORMAL|FILE_FLAG_WRITE_THROUGH;
+               break;
+       case MDB_O_COPY:                        /* mdb_env_copy() & co */
+               acc = GENERIC_WRITE;
+               share = 0;
+               disp = CREATE_NEW;
+               attrs = FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH;
+               break;
+       default: break; /* silence gcc -Wswitch (not all enum values handled) */
+       }
+       fd = CreateFileW(fname->mn_val, acc, share, NULL, disp, attrs, NULL);
+#else
+       fd = open(fname->mn_val, which & MDB_O_MASK, mode);
+#endif
+
+       if (fd == INVALID_HANDLE_VALUE)
+               rc = ErrCode();
+#ifndef _WIN32
+       else {
+               if (which != MDB_O_RDONLY && which != MDB_O_RDWR) {
+                       /* Set CLOEXEC if we could not pass it to open() */
+                       if (!MDB_CLOEXEC && (flags = fcntl(fd, F_GETFD)) != -1)
+                               (void) fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+               }
+               if (which == MDB_O_COPY && env->me_psize >= env->me_os_psize) {
+                       /* This may require buffer alignment.  There is no portable
+                        * way to ask how much, so we require OS pagesize alignment.
+                        */
+# ifdef F_NOCACHE      /* __APPLE__ */
+                       (void) fcntl(fd, F_NOCACHE, 1);
+# elif defined O_DIRECT
+                       /* open(...O_DIRECT...) would break on filesystems without
+                        * O_DIRECT support (ITS#7682). Try to set it here instead.
+                        */
+                       if ((flags = fcntl(fd, F_GETFL)) != -1)
+                               (void) fcntl(fd, F_SETFL, flags | O_DIRECT);
+# endif
+               }
+       }
+#endif /* !_WIN32 */
+
+       *res = fd;
+       return rc;
+}
+
+
 #ifdef BROKEN_FDATASYNC
 #include <sys/utsname.h>
 #include <sys/vfs.h>
@@ -4244,7 +4447,11 @@ mdb_env_reader_dest(void *ptr)
 {
        MDB_reader *reader = ptr;
 
-       reader->mr_pid = 0;
+#ifndef _WIN32
+       if (reader->mr_pid == getpid()) /* catch pthread_exit() in child process */
+#endif
+               /* We omit the mutex, so do this atomically (i.e. skip mr_txnid) */
+               reader->mr_pid = 0;
 }
 
 #ifdef _WIN32
@@ -4484,52 +4691,30 @@ mdb_hash_enc(MDB_val *val, char *encbuf)
 
 /** Open and/or initialize the lock region for the environment.
  * @param[in] env The LMDB environment.
- * @param[in] lpath The pathname of the file used for the lock region.
+ * @param[in] fname Filename + scratch area, from #mdb_fname_init().
  * @param[in] mode The Unix permissions for the file, if we create it.
  * @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 ESECT
-mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
+mdb_env_setup_locks(MDB_env *env, MDB_name *fname, 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
-#      define MDB_CLOEXEC              0
-#endif
-       int fdflags;
 #endif
        int rc;
        off_t size, rsize;
 
-#ifdef _WIN32
-       wchar_t *wlpath;
-       rc = utf8_to_utf16(lpath, -1, &wlpath, NULL);
-       if (rc)
-               return rc;
-       env->me_lfd = CreateFileW(wlpath, GENERIC_READ|GENERIC_WRITE,
-               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS,
-               FILE_ATTRIBUTE_NORMAL, NULL);
-       free(wlpath);
-#else
-       env->me_lfd = open(lpath, O_RDWR|O_CREAT|MDB_CLOEXEC, mode);
-#endif
-       if (env->me_lfd == INVALID_HANDLE_VALUE) {
-               rc = ErrCode();
+       rc = mdb_fopen(env, fname, MDB_O_LOCKS, mode, &env->me_lfd);
+       if (rc) {
+               /* Omit lockfile if read-only env on read-only filesystem */
                if (rc == MDB_ERRCODE_ROFS && (env->me_flags & MDB_RDONLY)) {
                        return MDB_SUCCESS;
                }
                goto fail;
        }
-#ifndef _WIN32
-       /* Lose record locks when exec*() */
-       if (!(MDB_CLOEXEC) && (fdflags = fcntl(env->me_lfd, F_GETFD)) != -1)
-                       fcntl(env->me_lfd, F_SETFD, fdflags | FD_CLOEXEC);
-#endif
 
        if (!(env->me_flags & MDB_NOTLS)) {
                rc = pthread_key_create(&env->me_txkey, mdb_env_reader_dest);
@@ -4719,12 +4904,6 @@ fail:
        return rc;
 }
 
-       /** The name of the lock file in the DB environment */
-#define LOCKNAME       "/lock.mdb"
-       /** The name of the data file in the DB environment */
-#define DATANAME       "/data.mdb"
-       /** The suffix of the lock file when no subdir is used */
-#define LOCKSUFF       "-lock"
        /** Only a subset of the @ref mdb_env flags can be changed
         *      at runtime. Changing other flags requires closing the
         *      environment and re-opening it with the new flags.
@@ -4740,36 +4919,18 @@ fail:
 int ESECT
 mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode)
 {
-       int             oflags, rc, len, excl = -1;
-       char *lpath, *dpath;
-#ifdef _WIN32
-       wchar_t *wpath;
-#endif
+       int rc, excl = -1;
+       MDB_name fname;
 
        if (env->me_fd!=INVALID_HANDLE_VALUE || (flags & ~(CHANGEABLE|CHANGELESS)))
                return EINVAL;
 
-       len = strlen(path);
-       if (flags & MDB_NOSUBDIR) {
-               rc = len + sizeof(LOCKSUFF) + len + 1;
-       } else {
-               rc = len + sizeof(LOCKNAME) + len + sizeof(DATANAME);
-       }
-       lpath = malloc(rc);
-       if (!lpath)
-               return ENOMEM;
-       if (flags & MDB_NOSUBDIR) {
-               dpath = lpath + len + sizeof(LOCKSUFF);
-               sprintf(lpath, "%s" LOCKSUFF, path);
-               strcpy(dpath, path);
-       } else {
-               dpath = lpath + len + sizeof(LOCKNAME);
-               sprintf(lpath, "%s" LOCKNAME, path);
-               sprintf(dpath, "%s" DATANAME, path);
-       }
-
-       rc = MDB_SUCCESS;
        flags |= env->me_flags;
+
+       rc = mdb_fname_init(path, flags, &fname);
+       if (rc)
+               return rc;
+
        if (flags & MDB_RDONLY) {
                /* silently ignore WRITEMAP when we're only getting read access */
                flags &= ~MDB_WRITEMAP;
@@ -4794,69 +4955,31 @@ mdb_env_open(MDB_env *env, const char *path, unsigned int flags, mdb_mode_t mode
 
        /* For RDONLY, get lockfile after we know datafile exists */
        if (!(flags & (MDB_RDONLY|MDB_NOLOCK))) {
-               rc = mdb_env_setup_locks(env, lpath, mode, &excl);
+               rc = mdb_env_setup_locks(env, &fname, mode, &excl);
                if (rc)
                        goto leave;
        }
 
-#ifdef _WIN32
-       if (F_ISSET(flags, MDB_RDONLY)) {
-               oflags = GENERIC_READ;
-               len = OPEN_EXISTING;
-       } else {
-               oflags = GENERIC_READ|GENERIC_WRITE;
-               len = OPEN_ALWAYS;
-       }
-       mode = FILE_ATTRIBUTE_NORMAL;
-       rc = utf8_to_utf16(dpath, -1, &wpath, NULL);
+       rc = mdb_fopen(env, &fname,
+               (flags & MDB_RDONLY) ? MDB_O_RDONLY : MDB_O_RDWR,
+               mode, &env->me_fd);
        if (rc)
                goto leave;
-       env->me_fd = CreateFileW(wpath, oflags, FILE_SHARE_READ|FILE_SHARE_WRITE,
-               NULL, len, mode, NULL);
-       free(wpath);
-#else
-       if (F_ISSET(flags, MDB_RDONLY))
-               oflags = O_RDONLY;
-       else
-               oflags = O_RDWR | O_CREAT;
-
-       env->me_fd = open(dpath, oflags, mode);
-#endif
-       if (env->me_fd == INVALID_HANDLE_VALUE) {
-               rc = ErrCode();
-               goto leave;
-       }
 
        if ((flags & (MDB_RDONLY|MDB_NOLOCK)) == MDB_RDONLY) {
-               rc = mdb_env_setup_locks(env, lpath, mode, &excl);
+               rc = mdb_env_setup_locks(env, &fname, mode, &excl);
                if (rc)
                        goto leave;
        }
 
        if ((rc = mdb_env_open2(env)) == MDB_SUCCESS) {
-               if (flags & (MDB_RDONLY|MDB_WRITEMAP)) {
-                       env->me_mfd = env->me_fd;
-               } else {
+               if (!(flags & (MDB_RDONLY|MDB_WRITEMAP))) {
                        /* Synchronous fd for meta writes. Needed even with
                         * MDB_NOSYNC/MDB_NOMETASYNC, in case these get reset.
                         */
-#ifdef _WIN32
-                       len = OPEN_EXISTING;
-                       rc = utf8_to_utf16(dpath, -1, &wpath, NULL);
+                       rc = mdb_fopen(env, &fname, MDB_O_META, mode, &env->me_mfd);
                        if (rc)
                                goto leave;
-                       env->me_mfd = CreateFileW(wpath, oflags,
-                               FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, len,
-                               mode | FILE_FLAG_WRITE_THROUGH, NULL);
-                       free(wpath);
-#else
-                       oflags &= ~O_CREAT;
-                       env->me_mfd = open(dpath, oflags | MDB_DSYNC, mode);
-#endif
-                       if (env->me_mfd == INVALID_HANDLE_VALUE) {
-                               rc = ErrCode();
-                               goto leave;
-                       }
                }
                DPRINTF(("opened dbenv %p", (void *) env));
                if (excl > 0) {
@@ -4889,7 +5012,7 @@ leave:
        if (rc) {
                mdb_env_close0(env, excl);
        }
-       free(lpath);
+       mdb_fname_destroy(fname);
        return rc;
 }
 
@@ -4933,7 +5056,7 @@ mdb_env_close0(MDB_env *env, int excl)
        if (env->me_map) {
                munmap(env->me_map, env->me_mapsize);
        }
-       if (env->me_mfd != env->me_fd && env->me_mfd != INVALID_HANDLE_VALUE)
+       if (env->me_mfd != INVALID_HANDLE_VALUE)
                (void) close(env->me_mfd);
        if (env->me_fd != INVALID_HANDLE_VALUE)
                (void) close(env->me_fd);
@@ -5233,7 +5356,9 @@ mdb_cursor_pop(MDB_cursor *mc)
        }
 }
 
-/** Push a page onto the top of the cursor's stack. */
+/** Push a page onto the top of the cursor's stack.
+ * Set #MDB_TXN_ERROR on failure.
+ */
 static int
 mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
 {
@@ -5253,6 +5378,7 @@ mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
 }
 
 /** Find the address of the page corresponding to a given page number.
+ * Set #MDB_TXN_ERROR on failure.
  * @param[in] mc the cursor accessing the page.
  * @param[in] pgno the page number for the page to retrieve.
  * @param[out] ret address of a pointer where the page's address will be stored.
@@ -5337,8 +5463,17 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
 
                if (flags & (MDB_PS_FIRST|MDB_PS_LAST)) {
                        i = 0;
-                       if (flags & MDB_PS_LAST)
+                       if (flags & MDB_PS_LAST) {
                                i = NUMKEYS(mp) - 1;
+                               /* if already init'd, see if we're already in right place */
+                               if (mc->mc_flags & C_INITIALIZED) {
+                                       if (mc->mc_ki[mc->mc_top] == i) {
+                                               mc->mc_top = mc->mc_snum++;
+                                               mp = mc->mc_pg[mc->mc_top];
+                                               goto ready;
+                                       }
+                               }
+                       }
                } else {
                        int      exact;
                        node = mdb_node_search(mc, key, &exact);
@@ -5364,6 +5499,7 @@ mdb_page_search_root(MDB_cursor *mc, MDB_val *key, int flags)
                if ((rc = mdb_cursor_push(mc, mp)))
                        return rc;
 
+ready:
                if (flags & MDB_PS_MODIFY) {
                        if ((rc = mdb_page_touch(mc)) != 0)
                                return rc;
@@ -5689,15 +5825,20 @@ mdb_cursor_next(MDB_cursor *mc, MDB_val *key, MDB_val *data, MDB_cursor_op op)
        MDB_node        *leaf;
        int rc;
 
-       if ((mc->mc_flags & C_EOF) ||
-               ((mc->mc_flags & C_DEL) && op == MDB_NEXT_DUP)) {
+       if ((mc->mc_flags & C_DEL && op == MDB_NEXT_DUP))
                return MDB_NOTFOUND;
-       }
+
        if (!(mc->mc_flags & C_INITIALIZED))
                return mdb_cursor_first(mc, key, data);
 
        mp = mc->mc_pg[mc->mc_top];
 
+       if (mc->mc_flags & C_EOF) {
+               if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mp)-1)
+                       return MDB_NOTFOUND;
+               mc->mc_flags ^= C_EOF;
+       }
+
        if (mc->mc_db->md_flags & MDB_DUPSORT) {
                leaf = NODEPTR(mp, mc->mc_ki[mc->mc_top]);
                if (F_ISSET(leaf->mn_flags, F_DUPDATA)) {
@@ -6102,16 +6243,13 @@ mdb_cursor_last(MDB_cursor *mc, MDB_val *key, MDB_val *data)
        if (mc->mc_xcursor)
                mc->mc_xcursor->mx_cursor.mc_flags &= ~(C_INITIALIZED|C_EOF);
 
-       if (!(mc->mc_flags & C_EOF)) {
-
-               if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
-                       rc = mdb_page_search(mc, NULL, MDB_PS_LAST);
-                       if (rc != MDB_SUCCESS)
-                               return rc;
-               }
-               mdb_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
-
+       if (!(mc->mc_flags & C_INITIALIZED) || mc->mc_top) {
+               rc = mdb_page_search(mc, NULL, MDB_PS_LAST);
+               if (rc != MDB_SUCCESS)
+                       return rc;
        }
+       mdb_cassert(mc, IS_LEAF(mc->mc_pg[mc->mc_top]));
+
        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]);
@@ -6288,6 +6426,11 @@ fetchm:
                        rc = MDB_INCOMPATIBLE;
                        break;
                }
+               if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top])) {
+                       mc->mc_ki[mc->mc_top] = NUMKEYS(mc->mc_pg[mc->mc_top]);
+                       rc = MDB_NOTFOUND;
+                       break;
+               }
                {
                        MDB_node *leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
                        if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
@@ -6942,6 +7085,7 @@ mdb_cursor_del(MDB_cursor *mc, unsigned int flags)
                                                if (!(m2->mc_flags & C_INITIALIZED)) continue;
                                                if (m2->mc_pg[mc->mc_top] == mp) {
                                                        MDB_node *n2 = leaf;
+                                                       if (m2->mc_ki[mc->mc_top] >= NUMKEYS(mp)) continue;
                                                        if (m2->mc_ki[mc->mc_top] != mc->mc_ki[mc->mc_top]) {
                                                                n2 = NODEPTR(mp, m2->mc_ki[mc->mc_top]);
                                                                if (n2->mn_flags & F_SUBDATA) continue;
@@ -6991,6 +7135,7 @@ fail:
 }
 
 /** Allocate and initialize new pages for a database.
+ * Set #MDB_TXN_ERROR on failure.
  * @param[in] mc a cursor on the database being added to.
  * @param[in] flags flags defining what type of page is being allocated.
  * @param[in] num the number of pages to allocate. This is usually 1,
@@ -7076,6 +7221,7 @@ mdb_branch_size(MDB_env *env, MDB_val *key)
 }
 
 /** Add a node to the page pointed to by the cursor.
+ * Set #MDB_TXN_ERROR on failure.
  * @param[in] mc The cursor for this operation.
  * @param[in] indx The index on the page where the new node should be added.
  * @param[in] key The key for the new node.
@@ -7520,9 +7666,15 @@ mdb_cursor_count(MDB_cursor *mc, size_t *countp)
        if (!(mc->mc_flags & C_INITIALIZED))
                return EINVAL;
 
-       if (!mc->mc_snum || (mc->mc_flags & C_EOF))
+       if (!mc->mc_snum)
                return MDB_NOTFOUND;
 
+       if (mc->mc_flags & C_EOF) {
+               if (mc->mc_ki[mc->mc_top] >= NUMKEYS(mc->mc_pg[mc->mc_top]))
+                       return MDB_NOTFOUND;
+               mc->mc_flags ^= C_EOF;
+       }
+
        leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
        if (!F_ISSET(leaf->mn_flags, F_DUPDATA)) {
                *countp = 1;
@@ -7564,6 +7716,7 @@ mdb_cursor_dbi(MDB_cursor *mc)
 }
 
 /** Replace the key for a branch node with a new key.
+ * Set #MDB_TXN_ERROR on failure.
  * @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.
@@ -8308,15 +8461,20 @@ mdb_cursor_del0(MDB_cursor *mc)
                                        }
                                        if (mc->mc_db->md_flags & MDB_DUPSORT) {
                                                MDB_node *node = NODEPTR(m3->mc_pg[m3->mc_top], m3->mc_ki[m3->mc_top]);
-                                               /* If this node is a fake page, it needs to be reinited
-                                                * because its data has moved. But just reset mc_pg[0]
-                                                * if the xcursor is already live.
+                                               /* If this node has dupdata, it may need to be reinited
+                                                * because its data has moved.
+                                                * If the xcursor was not initd it must be reinited.
+                                                * Else if node points to a subDB, nothing is needed.
+                                                * Else (xcursor was initd, not a subDB) needs mc_pg[0] reset.
                                                 */
-                                               if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) == F_DUPDATA) {
-                                                       if (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED)
-                                                               m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
-                                                       else
+                                               if (node->mn_flags & F_DUPDATA) {
+                                                       if (m3->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
+                                                               if (!(node->mn_flags & F_SUBDATA))
+                                                                       m3->mc_xcursor->mx_cursor.mc_pg[0] = NODEDATA(node);
+                                                       } else {
                                                                mdb_xcursor_init1(m3, node);
+                                                               m3->mc_xcursor->mx_cursor.mc_flags |= C_DEL;
+                                                       }
                                                }
                                        }
                                }
@@ -8392,6 +8550,7 @@ mdb_del0(MDB_txn *txn, MDB_dbi dbi,
 }
 
 /** Split a page and insert a new node.
+ * Set #MDB_TXN_ERROR on failure.
  * @param[in,out] mc Cursor pointing to the page and desired insertion index.
  * The cursor will be updated to point to the actual page and index where
  * the node got inserted after the split.
@@ -8878,6 +9037,13 @@ mdb_env_copythr(void *arg)
 #else
        int len;
 #define DO_WRITE(rc, fd, ptr, w2, len) len = write(fd, ptr, w2); rc = (len >= 0)
+#ifdef SIGPIPE
+       sigset_t set;
+       sigemptyset(&set);
+       sigaddset(&set, SIGPIPE);
+       if ((rc = pthread_sigmask(SIG_BLOCK, &set, NULL)) != 0)
+               my->mc_error = rc;
+#endif
 #endif
 
        pthread_mutex_lock(&my->mc_mutex);
@@ -8894,6 +9060,15 @@ again:
                        DO_WRITE(rc, my->mc_fd, ptr, wsize, len);
                        if (!rc) {
                                rc = ErrCode();
+#if defined(SIGPIPE) && !defined(_WIN32)
+                               if (rc == EPIPE) {
+                                       /* Collect the pending SIGPIPE, otherwise at least OS X
+                                        * gives it to the process on thread-exit (ITS#8504).
+                                        */
+                                       int tmp;
+                                       sigwait(&set, &tmp);
+                               }
+#endif
                                break;
                        } else if (len > 0) {
                                rc = MDB_SUCCESS;
@@ -9356,62 +9531,20 @@ mdb_env_copyfd(MDB_env *env, HANDLE fd)
 int ESECT
 mdb_env_copy2(MDB_env *env, const char *path, unsigned int flags)
 {
-       int rc, len;
-       char *lpath;
+       int rc;
+       MDB_name fname;
        HANDLE newfd = INVALID_HANDLE_VALUE;
-#ifdef _WIN32
-       wchar_t *wpath;
-#endif
-
-       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
-       rc = utf8_to_utf16(lpath, -1, &wpath, NULL);
-       if (rc)
-               goto leave;
-       newfd = CreateFileW(wpath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
-                               FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH, NULL);
-       free(wpath);
-#else
-       newfd = open(lpath, O_WRONLY|O_CREAT|O_EXCL, 0666);
-#endif
-       if (newfd == INVALID_HANDLE_VALUE) {
-               rc = ErrCode();
-               goto leave;
-       }
-
-       if (env->me_psize >= env->me_os_psize) {
-#ifdef F_NOCACHE       /* __APPLE__ */
-       (void) fcntl(newfd, F_NOCACHE, 1);
-#elif defined O_DIRECT
-       /* Set O_DIRECT if the file system supports it */
-       if ((rc = fcntl(newfd, F_GETFL)) != -1)
-               (void) fcntl(newfd, F_SETFL, rc | O_DIRECT);
-#endif
+       rc = mdb_fname_init(path, env->me_flags | MDB_NOLOCK, &fname);
+       if (rc == MDB_SUCCESS) {
+               rc = mdb_fopen(env, &fname, MDB_O_COPY, 0666, &newfd);
+               mdb_fname_destroy(fname);
        }
-
-       rc = mdb_env_copyfd2(env, newfd, flags);
-
-leave:
-       if (!(env->me_flags & MDB_NOSUBDIR))
-               free(lpath);
-       if (newfd != INVALID_HANDLE_VALUE)
+       if (rc == MDB_SUCCESS) {
+               rc = mdb_env_copyfd2(env, newfd, flags);
                if (close(newfd) < 0 && rc == MDB_SUCCESS)
                        rc = ErrCode();
-
+       }
        return rc;
 }
 
@@ -9633,8 +9766,11 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
                MDB_node *node = NODEPTR(mc.mc_pg[mc.mc_top], mc.mc_ki[mc.mc_top]);
                if ((node->mn_flags & (F_DUPDATA|F_SUBDATA)) != F_SUBDATA)
                        return MDB_INCOMPATIBLE;
-       } else if (! (rc == MDB_NOTFOUND && (flags & MDB_CREATE))) {
-               return rc;
+       } else {
+               if (rc != MDB_NOTFOUND || !(flags & MDB_CREATE))
+                       return rc;
+               if (F_ISSET(txn->mt_flags, MDB_TXN_RDONLY))
+                       return EACCES;
        }
 
        /* Done here so we cannot fail after creating a new DB */
@@ -9648,7 +9784,8 @@ int mdb_dbi_open(MDB_txn *txn, const char *name, unsigned int flags, MDB_dbi *db
                memset(&dummy, 0, sizeof(dummy));
                dummy.md_root = P_INVALID;
                dummy.md_flags = flags & PERSISTENT_FLAGS;
-               rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA);
+               WITH_CURSOR_TRACKING(mc,
+                       rc = mdb_cursor_put(&mc, &key, &data, F_SUBDATA));
                dbflag |= DB_DIRTY;
        }
 
@@ -10001,7 +10138,7 @@ mdb_reader_check(MDB_env *env, int *dead)
        return env->me_txns ? mdb_reader_check0(env, 0, dead) : MDB_SUCCESS;
 }
 
-/** As #mdb_reader_check(). rlocked = <caller locked the reader mutex>. */
+/** As #mdb_reader_check(). \b rlocked is set if caller locked #me_rmutex. */
 static int ESECT
 mdb_reader_check0(MDB_env *env, int rlocked, int *dead)
 {
@@ -10104,25 +10241,32 @@ mdb_mutex_failed(MDB_env *env, mdb_mutexref_t mutex, int rc)
        return rc;
 }
 #endif /* MDB_ROBUST_SUPPORTED */
-/** @} */
 
 #if defined(_WIN32)
-static int utf8_to_utf16(const char *src, int srcsize, wchar_t **dst, int *dstsize)
-{
-       int need;
-       wchar_t *result;
-       need = MultiByteToWideChar(CP_UTF8, 0, src, srcsize, NULL, 0);
-       if (need == 0xFFFD)
-               return EILSEQ;
-       if (need == 0)
-               return EINVAL;
-       result = malloc(sizeof(wchar_t) * need);
-       if (!result)
-               return ENOMEM;
-       MultiByteToWideChar(CP_UTF8, 0, src, srcsize, result, need);
-       if (dstsize)
-               *dstsize = need;
-       *dst = result;
-       return 0;
+/** Convert \b src to new wchar_t[] string with room for \b xtra extra chars */
+static int ESECT
+utf8_to_utf16(const char *src, MDB_name *dst, int xtra)
+{
+       int rc, need = 0;
+       wchar_t *result = NULL;
+       for (;;) {                                      /* malloc result, then fill it in */
+               need = MultiByteToWideChar(CP_UTF8, 0, src, -1, result, need);
+               if (!need) {
+                       rc = ErrCode();
+                       free(result);
+                       return rc;
+               }
+               if (!result) {
+                       result = malloc(sizeof(wchar_t) * (need + xtra));
+                       if (!result)
+                               return ENOMEM;
+                       continue;
+               }
+               dst->mn_alloced = 1;
+               dst->mn_len = need - 1;
+               dst->mn_val = result;
+               return MDB_SUCCESS;
+       }
 }
 #endif /* defined(_WIN32) */
+/** @} */