]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
ITS#8109 fix mdb_cursor_del0 on empty DB
[openldap] / libraries / liblmdb / mdb.c
index 264c15fc9fe8fdcbdf738bb754178f7cc47e7a32..fe651509f8a1debc766c95fdfeb8a4a48806f677 100644 (file)
@@ -5,7 +5,7 @@
  *     BerkeleyDB API, but much simplified.
  */
 /*
- * Copyright 2011-2014 Howard Chu, Symas Corp.
+ * Copyright 2011-2015 Howard Chu, Symas Corp.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -79,6 +79,14 @@ extern int cacheflush(char *addr, int nbytes, int cache);
 #define CACHEFLUSH(addr, bytes, cache)
 #endif
 
+#if defined(__linux) && !defined(MDB_FDATASYNC_WORKS)
+/** fdatasync is broken on ext3/ext4fs on older kernels, see
+ *     description in #mdb_env_open2 comments. You can safely
+ *     define MDB_FDATASYNC_WORKS if this code will only be run
+ *     on kernels 3.6 and newer.
+ */
+#define        BROKEN_FDATASYNC
+#endif
 
 #include <errno.h>
 #include <limits.h>
@@ -580,11 +588,11 @@ typedef struct MDB_rxbody {
         *      started from so we can avoid overwriting any data used in that
         *      particular version.
         */
-       txnid_t         mrb_txnid;
+       volatile txnid_t                mrb_txnid;
        /** The process ID of the process owning this reader txn. */
-       MDB_PID_T       mrb_pid;
+       volatile MDB_PID_T      mrb_pid;
        /** The thread ID of the thread owning this txn. */
-       MDB_THR_T       mrb_tid;
+       volatile MDB_THR_T      mrb_tid;
 } MDB_rxbody;
 
        /** The actual reader record, with cacheline padding. */
@@ -632,12 +640,12 @@ typedef struct MDB_txbody {
                 *      This is recorded here only for convenience; the value can always
                 *      be determined by reading the main database meta pages.
                 */
-       txnid_t         mtb_txnid;
+       volatile txnid_t                mtb_txnid;
                /** The number of slots that have been used in the reader table.
                 *      This always records the maximum count, it is not decremented
                 *      when readers release their slots.
                 */
-       unsigned        mtb_numreaders;
+       volatile unsigned       mtb_numreaders;
 } MDB_txbody;
 
        /** The actual reader table definition. */
@@ -908,7 +916,7 @@ typedef struct MDB_meta {
        /** Any persistent environment flags. @ref mdb_env */
 #define        mm_flags        mm_dbs[0].md_flags
        pgno_t          mm_last_pg;                     /**< last used page in file */
-       txnid_t         mm_txnid;                       /**< txnid that committed this page */
+       volatile txnid_t        mm_txnid;       /**< txnid that committed this page */
 } MDB_meta;
 
        /** Buffer for a stack-allocated meta page.
@@ -1096,6 +1104,8 @@ struct MDB_env {
 #define        MDB_ENV_ACTIVE  0x20000000U
        /** me_txkey is set */
 #define        MDB_ENV_TXKEY   0x10000000U
+       /** fdatasync is unreliable */
+#define        MDB_FSYNCONLY   0x08000000U
        uint32_t        me_flags;               /**< @ref 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 */
@@ -1142,9 +1152,6 @@ struct MDB_env {
 #elif defined(MDB_USE_POSIX_SEM)
        sem_t           *me_rmutex;             /* Shared mutexes are not supported */
        sem_t           *me_wmutex;
-#endif
-#ifdef __linux
-       int             me_fsynconly;           /**< fdatasync is unreliable */
 #endif
        void            *me_userctx;     /**< User-settable context */
        MDB_assert_func *me_assert_func; /**< Callback for assertion failures */
@@ -2305,6 +2312,8 @@ int
 mdb_env_sync(MDB_env *env, int force)
 {
        int rc = 0;
+       if (env->me_flags & MDB_RDONLY)
+               return EACCES;
        if (force || !F_ISSET(env->me_flags, MDB_NOSYNC)) {
                if (env->me_flags & MDB_WRITEMAP) {
                        int flags = ((env->me_flags & MDB_MAPASYNC) && !force)
@@ -2316,8 +2325,8 @@ mdb_env_sync(MDB_env *env, int force)
                                rc = ErrCode();
 #endif
                } else {
-#ifdef __linux
-                       if (env->me_fsynconly) {
+#ifdef BROKEN_FDATASYNC
+                       if (env->me_flags & MDB_FSYNCONLY) {
                                if (fsync(env->me_fd))
                                        rc = ErrCode();
                        } else
@@ -2482,6 +2491,7 @@ mdb_txn_renew0(MDB_txn *txn)
        int rc, new_notls = 0;
 
        if (txn->mt_flags & MDB_TXN_RDONLY) {
+               txn->mt_flags = MDB_TXN_RDONLY;
                /* Setup db info */
                txn->mt_numdbs = env->me_numdbs;
                txn->mt_dbxs = env->me_dbxs;    /* mostly static anyway */
@@ -2530,7 +2540,10 @@ mdb_txn_renew0(MDB_txn *txn)
                                        return rc;
                                }
                        }
-                       txn->mt_txnid = r->mr_txnid = ti->mti_txnid;
+                       do /* LY: Retry on a race, ITS#7970. */
+                               r->mr_txnid = ti->mti_txnid;
+                       while(r->mr_txnid != ti->mti_txnid);
+                       txn->mt_txnid = r->mr_txnid;
                        txn->mt_u.reader = r;
                        meta = env->me_metas[txn->mt_txnid & 1];
                }
@@ -3120,6 +3133,7 @@ mdb_page_flush(MDB_txn *txn, int keep)
                /* Write up to MDB_COMMIT_PAGES dirty pages at a time. */
                if (pos!=next_pos || n==MDB_COMMIT_PAGES || wsize+size>MAX_WRITE) {
                        if (n) {
+retry_write:
                                /* Write previous page(s) */
 #ifdef MDB_USE_PWRITEV
                                wres = pwritev(env->me_fd, iov, n, wpos);
@@ -3127,8 +3141,11 @@ mdb_page_flush(MDB_txn *txn, int keep)
                                if (n == 1) {
                                        wres = pwrite(env->me_fd, iov[0].iov_base, wsize, wpos);
                                } else {
+retry_seek:
                                        if (lseek(env->me_fd, wpos, SEEK_SET) == -1) {
                                                rc = ErrCode();
+                                               if (rc == EINTR)
+                                                       goto retry_seek;
                                                DPRINTF(("lseek: %s", strerror(rc)));
                                                return rc;
                                        }
@@ -3138,6 +3155,8 @@ mdb_page_flush(MDB_txn *txn, int keep)
                                if (wres != wsize) {
                                        if (wres < 0) {
                                                rc = ErrCode();
+                                               if (rc == EINTR)
+                                                       goto retry_write;
                                                DPRINTF(("Write error: %s", strerror(rc)));
                                        } else {
                                                rc = EIO; /* TODO: Use which error code? */
@@ -3507,7 +3526,8 @@ mdb_env_init_meta(MDB_env *env, MDB_meta *meta)
        int len;
 #define DO_PWRITE(rc, fd, ptr, size, len, pos) do { \
        len = pwrite(fd, ptr, size, pos);       \
-       rc = (len >= 0); } while(0)
+       if (len == -1 && ErrCode() == EINTR) continue; \
+       rc = (len >= 0); break; } while(1)
 #endif
 
        DPUTS("writing new meta page");
@@ -3612,6 +3632,7 @@ mdb_env_write_meta(MDB_txn *txn)
        /* Write to the SYNC fd */
        mfd = env->me_flags & (MDB_NOSYNC|MDB_NOMETASYNC) ?
                env->me_fd : env->me_mfd;
+retry_write:
 #ifdef _WIN32
        {
                memset(&ov, 0, sizeof(ov));
@@ -3624,6 +3645,8 @@ mdb_env_write_meta(MDB_txn *txn)
 #endif
        if (rc != len) {
                rc = rc < 0 ? ErrCode() : EIO;
+               if (rc == EINTR)
+                       goto retry_write;
                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.
@@ -3859,7 +3882,7 @@ mdb_fsize(HANDLE fd, size_t *size)
        return MDB_SUCCESS;
 }
 
-#ifdef __linux
+#ifdef BROKEN_FDATASYNC
 #include <sys/utsname.h>
 #include <sys/vfs.h>
 #endif
@@ -3881,7 +3904,7 @@ mdb_env_open2(MDB_env *env)
        else
                env->me_pidquery = PROCESS_QUERY_INFORMATION;
 #endif /* _WIN32 */
-#ifdef __linux
+#ifdef BROKEN_FDATASYNC
        /* ext3/ext4 fdatasync is broken on some older Linux kernels.
         * https://lkml.org/lkml/2012/9/3/83
         * Kernels after 3.6-rc6 are known good.
@@ -3923,7 +3946,7 @@ mdb_env_open2(MDB_env *env)
                        } else {        /* 4.x and newer is OK */
                                break;
                        }
-                       env->me_fsynconly = 1;
+                       env->me_flags |= MDB_FSYNCONLY;
                        break;
                }
        }
@@ -4638,13 +4661,15 @@ mdb_env_close0(MDB_env *env, int excl)
                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);
+       if (env->me_dbxs) {
+               for (i = env->me_maxdbs; --i > MAIN_DBI; )
+                       free(env->me_dbxs[i].md_name.mv_data);
+               free(env->me_dbxs);
+       }
 
        free(env->me_pbuf);
        free(env->me_dbiseqs);
        free(env->me_dbflags);
-       free(env->me_dbxs);
        free(env->me_path);
        free(env->me_dirty_list);
        free(env->me_txn0);
@@ -7074,6 +7099,7 @@ mdb_cursor_init(MDB_cursor *mc, MDB_txn *txn, MDB_dbi dbi, MDB_xcursor *mx)
        mc->mc_snum = 0;
        mc->mc_top = 0;
        mc->mc_pg[0] = 0;
+       mc->mc_ki[0] = 0;
        mc->mc_flags = 0;
        if (txn->mt_dbs[dbi].md_flags & MDB_DUPSORT) {
                mdb_tassert(txn, mx != NULL);
@@ -7447,7 +7473,7 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                        cdst->mc_ki[cdst->mc_top] = 0;
                        rc = mdb_update_key(cdst, &nullkey);
                        cdst->mc_ki[cdst->mc_top] = ix;
-                       mdb_cassert(csrc, rc == MDB_SUCCESS);
+                       mdb_cassert(cdst, rc == MDB_SUCCESS);
                }
        }
 
@@ -7703,12 +7729,12 @@ mdb_rebalance(MDB_cursor *mc)
                                                m3 = m2;
                                        if (m3 == mc || m3->mc_snum < mc->mc_snum) continue;
                                        if (m3->mc_pg[0] == mp) {
-                                               m3->mc_snum--;
-                                               m3->mc_top--;
                                                for (i=0; i<m3->mc_snum; i++) {
                                                        m3->mc_pg[i] = m3->mc_pg[i+1];
                                                        m3->mc_ki[i] = m3->mc_ki[i+1];
                                                }
+                                               m3->mc_snum--;
+                                               m3->mc_top--;
                                        }
                                }
                        }
@@ -7776,9 +7802,23 @@ mdb_rebalance(MDB_cursor *mc)
                if (mc->mc_ki[ptop] == 0) {
                        rc = mdb_page_merge(&mn, mc);
                } else {
+                       MDB_cursor dummy;
                        oldki += NUMKEYS(mn.mc_pg[mn.mc_top]);
                        mn.mc_ki[mn.mc_top] += mc->mc_ki[mn.mc_top] + 1;
+                       /* We want mdb_rebalance to find mn when doing fixups */
+                       if (mc->mc_flags & C_SUB) {
+                               dummy.mc_next = mc->mc_txn->mt_cursors[mc->mc_dbi];
+                               mc->mc_txn->mt_cursors[mc->mc_dbi] = &dummy;
+                               dummy.mc_xcursor = (MDB_xcursor *)&mn;
+                       } else {
+                               mn.mc_next = mc->mc_txn->mt_cursors[mc->mc_dbi];
+                               mc->mc_txn->mt_cursors[mc->mc_dbi] = &mn;
+                       }
                        rc = mdb_page_merge(mc, &mn);
+                       if (mc->mc_flags & C_SUB)
+                               mc->mc_txn->mt_cursors[mc->mc_dbi] = dummy.mc_next;
+                       else
+                               mc->mc_txn->mt_cursors[mc->mc_dbi] = mn.mc_next;
                        mdb_cursor_copy(&mn, mc);
                }
                mc->mc_flags &= ~C_EOF;
@@ -7805,6 +7845,13 @@ mdb_cursor_del0(MDB_cursor *mc)
                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.
+                */
+               if (!mc->mc_snum)
+                       return rc;
+
                mp = mc->mc_pg[mc->mc_top];
                nkeys = NUMKEYS(mp);