]> git.sur5r.net Git - openldap/blobdiff - libraries/liblmdb/mdb.c
ITS#7718 invalidate cursor when DB gets emptied
[openldap] / libraries / liblmdb / mdb.c
index 30b562c43e6c2d64b4c43303677f073566b2026b..e935dc633aa8d0917855ef94935c7c9d1f78e8db 100644 (file)
@@ -317,6 +317,9 @@ static txnid_t mdb_debug_start;
         *      The string is printed literally, with no format processing.
         */
 #define DPUTS(arg)     DPRINTF(("%s", arg))
+       /** Debuging output value of a cursor DBI: Negative in a sub-cursor. */
+#define DDBI(mc) \
+       (((mc)->mc_flags & C_SUB) ? -(int)(mc)->mc_dbi : (int)(mc)->mc_dbi)
 /** @} */
 
        /** A default memory page size.
@@ -869,7 +872,7 @@ struct MDB_txn {
  *     @ingroup internal
  * @{
  */
-#define DB_DIRTY       0x01            /**< DB was written in this txn */
+#define DB_DIRTY       0x01            /**< DB was modified or is DUPSORT data */
 #define DB_STALE       0x02            /**< Named-DB record is older than txnID */
 #define DB_NEW         0x04            /**< Named-DB handle opened in this txn */
 #define DB_VALID       0x08            /**< DB handle is valid, see also #MDB_VALID */
@@ -1859,7 +1862,6 @@ mdb_page_touch(MDB_cursor *mc)
        MDB_page *mp = mc->mc_pg[mc->mc_top], *np;
        MDB_txn *txn = mc->mc_txn;
        MDB_cursor *m2, *m3;
-       MDB_dbi dbi;
        pgno_t  pgno;
        int rc;
 
@@ -1876,7 +1878,8 @@ mdb_page_touch(MDB_cursor *mc)
                        (rc = mdb_page_alloc(mc, 1, &np)))
                        return rc;
                pgno = np->mp_pgno;
-               DPRINTF(("touched db %u page %"Z"u -> %"Z"u", mc->mc_dbi,mp->mp_pgno,pgno));
+               DPRINTF(("touched db %d page %"Z"u -> %"Z"u", DDBI(mc),
+                       mp->mp_pgno, pgno));
                assert(mp->mp_pgno != pgno);
                mdb_midl_xappend(txn->mt_free_pgs, mp->mp_pgno);
                /* Update the parent page, if any, to point to the new page */
@@ -1922,17 +1925,16 @@ mdb_page_touch(MDB_cursor *mc)
 done:
        /* Adjust cursors pointing to mp */
        mc->mc_pg[mc->mc_top] = np;
-       dbi = mc->mc_dbi;
+       m2 = txn->mt_cursors[mc->mc_dbi];
        if (mc->mc_flags & C_SUB) {
-               dbi--;
-               for (m2 = txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+               for (; m2; m2=m2->mc_next) {
                        m3 = &m2->mc_xcursor->mx_cursor;
                        if (m3->mc_snum < mc->mc_snum) continue;
                        if (m3->mc_pg[mc->mc_top] == mp)
                                m3->mc_pg[mc->mc_top] = np;
                }
        } else {
-               for (m2 = txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
+               for (; m2; m2=m2->mc_next) {
                        if (m2->mc_snum < mc->mc_snum) continue;
                        if (m2->mc_pg[mc->mc_top] == mp) {
                                m2->mc_pg[mc->mc_top] = np;
@@ -2117,8 +2119,9 @@ static int
 mdb_txn_renew0(MDB_txn *txn)
 {
        MDB_env *env = txn->mt_env;
+       MDB_txninfo *ti = env->me_txns;
        MDB_meta *meta;
-       unsigned int i;
+       unsigned int i, nr;
        uint16_t x;
        int rc, new_notls = 0;
 
@@ -2127,7 +2130,7 @@ mdb_txn_renew0(MDB_txn *txn)
        txn->mt_dbxs = env->me_dbxs;    /* mostly static anyway */
 
        if (txn->mt_flags & MDB_TXN_RDONLY) {
-               if (!env->me_txns) {
+               if (!ti) {
                        meta = env->me_metas[ mdb_env_pick_meta(env) ];
                        txn->mt_txnid = meta->mm_txnid;
                        txn->mt_u.reader = NULL;
@@ -2151,36 +2154,38 @@ mdb_txn_renew0(MDB_txn *txn)
                                }
 
                                LOCK_MUTEX_R(env);
-                               for (i=0; i<env->me_txns->mti_numreaders; i++)
-                                       if (env->me_txns->mti_readers[i].mr_pid == 0)
+                               nr = ti->mti_numreaders;
+                               for (i=0; i<nr; i++)
+                                       if (ti->mti_readers[i].mr_pid == 0)
                                                break;
                                if (i == env->me_maxreaders) {
                                        UNLOCK_MUTEX_R(env);
                                        return MDB_READERS_FULL;
                                }
-                               env->me_txns->mti_readers[i].mr_pid = pid;
-                               env->me_txns->mti_readers[i].mr_tid = tid;
-                               if (i >= env->me_txns->mti_numreaders)
-                                       env->me_txns->mti_numreaders = i+1;
+                               ti->mti_readers[i].mr_pid = pid;
+                               ti->mti_readers[i].mr_tid = tid;
+                               if (i == nr)
+                                       ti->mti_numreaders = ++nr;
                                /* Save numreaders for un-mutexed mdb_env_close() */
-                               env->me_numreaders = env->me_txns->mti_numreaders;
+                               env->me_numreaders = nr;
                                UNLOCK_MUTEX_R(env);
-                               r = &env->me_txns->mti_readers[i];
+
+                               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;
                                        return rc;
                                }
                        }
-                       txn->mt_txnid = r->mr_txnid = env->me_txns->mti_txnid;
+                       txn->mt_txnid = r->mr_txnid = ti->mti_txnid;
                        txn->mt_u.reader = r;
                        meta = env->me_metas[txn->mt_txnid & 1];
                }
        } else {
-               if (env->me_txns) {
+               if (ti) {
                        LOCK_MUTEX_W(env);
 
-                       txn->mt_txnid = env->me_txns->mti_txnid;
+                       txn->mt_txnid = ti->mti_txnid;
                        meta = env->me_metas[txn->mt_txnid & 1];
                } else {
                        meta = env->me_metas[ mdb_env_pick_meta(env) ];
@@ -3267,7 +3272,7 @@ mdb_env_map(MDB_env *env, void *addr, int newsize)
        int prot = PROT_READ;
        if (flags & MDB_WRITEMAP) {
                prot |= PROT_WRITE;
-               if (newsize && ftruncate(env->me_fd, env->me_mapsize) < 0)
+               if (ftruncate(env->me_fd, env->me_mapsize) < 0)
                        return ErrCode();
        }
        env->me_map = mmap(addr, env->me_mapsize, prot, MAP_SHARED,
@@ -3314,6 +3319,14 @@ mdb_env_set_mapsize(MDB_env *env, size_t size)
                        return EINVAL;
                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;
+                       if (size < minsize)
+                               size = minsize;
+               }
                munmap(env->me_map, env->me_mapsize);
                env->me_mapsize = size;
                old = (env->me_flags & MDB_FIXEDMAP) ? env->me_map : NULL;
@@ -4502,8 +4515,8 @@ mdb_cursor_pop(MDB_cursor *mc)
                if (mc->mc_snum)
                        mc->mc_top--;
 
-               DPRINTF(("popped page %"Z"u off db %u cursor %p", top->mp_pgno,
-                       mc->mc_dbi, (void *) mc));
+               DPRINTF(("popped page %"Z"u off db %d cursor %p", top->mp_pgno,
+                       DDBI(mc), (void *) mc));
        }
 }
 
@@ -4511,8 +4524,8 @@ mdb_cursor_pop(MDB_cursor *mc)
 static int
 mdb_cursor_push(MDB_cursor *mc, MDB_page *mp)
 {
-       DPRINTF(("pushing page %"Z"u on db %u cursor %p", mp->mp_pgno,
-               mc->mc_dbi, (void *) mc));
+       DPRINTF(("pushing page %"Z"u on db %d cursor %p", mp->mp_pgno,
+               DDBI(mc), (void *) mc));
 
        if (mc->mc_snum >= CURSOR_STACK) {
                assert(mc->mc_snum < CURSOR_STACK);
@@ -4745,8 +4758,8 @@ mdb_page_search(MDB_cursor *mc, MDB_val *key, int flags)
        mc->mc_snum = 1;
        mc->mc_top = 0;
 
-       DPRINTF(("db %u root page %"Z"u has flags 0x%X",
-               mc->mc_dbi, root, mc->mc_pg[0]->mp_flags));
+       DPRINTF(("db %d root page %"Z"u has flags 0x%X",
+               DDBI(mc), root, mc->mc_pg[0]->mp_flags));
 
        if (flags & MDB_PS_MODIFY) {
                if ((rc = mdb_page_touch(mc)))
@@ -4937,8 +4950,11 @@ 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, NULL) != 0))
+       if ((rc = mdb_page_get(mc->mc_txn, NODEPGNO(indx), &mp, NULL)) != 0) {
+               /* mc will be inconsistent if caller does mc_snum++ as above */
+               mc->mc_flags &= ~(C_INITIALIZED|C_EOF);
                return rc;
+       }
 
        mdb_cursor_push(mc, mp);
        if (!move_right)
@@ -5545,14 +5561,14 @@ fetchm:
        return rc;
 }
 
-/** Touch all the pages in the cursor stack.
+/** Touch all the pages in the cursor stack. Set mc_top.
  *     Makes sure all the pages are writable, before attempting a write operation.
  * @param[in] mc The cursor to operate on.
  */
 static int
 mdb_cursor_touch(MDB_cursor *mc)
 {
-       int rc;
+       int rc = MDB_SUCCESS;
 
        if (mc->mc_dbi > MAIN_DBI && !(*mc->mc_dbflag & DB_DIRTY)) {
                MDB_cursor mc2;
@@ -5563,13 +5579,14 @@ mdb_cursor_touch(MDB_cursor *mc)
                         return rc;
                *mc->mc_dbflag |= DB_DIRTY;
        }
-       for (mc->mc_top = 0; mc->mc_top < mc->mc_snum; mc->mc_top++) {
-               rc = mdb_page_touch(mc);
-               if (rc)
-                       return rc;
+       mc->mc_top = 0;
+       if (mc->mc_snum) {
+               do {
+                       rc = mdb_page_touch(mc);
+               } while (!rc && ++(mc->mc_top) < mc->mc_snum);
+               mc->mc_top = mc->mc_snum-1;
        }
-       mc->mc_top = mc->mc_snum-1;
-       return MDB_SUCCESS;
+       return rc;
 }
 
 /** Do not spill pages to disk if txn is getting full, may fail instead */
@@ -5620,8 +5637,8 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
                return MDB_BAD_VALSIZE;
 #endif
 
-       DPRINTF(("==> put db %u key [%s], size %"Z"u, data size %"Z"u",
-               mc->mc_dbi, DKEY(key), key ? key->mv_size:0, data->mv_size));
+       DPRINTF(("==> put db %d key [%s], size %"Z"u, data size %"Z"u",
+               DDBI(mc), DKEY(key), key ? key->mv_size : 0, data->mv_size));
 
        dkey.mv_size = 0;
 
@@ -5632,6 +5649,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
        } else if (mc->mc_db->md_root == P_INVALID) {
                /* new database, cursor has nothing to point to */
                mc->mc_snum = 0;
+               mc->mc_top = 0;
                mc->mc_flags &= ~C_INITIALIZED;
                rc = MDB_NO_ROOT;
        } else {
@@ -5950,9 +5968,6 @@ new_sub:
                        unsigned i = mc->mc_top;
                        MDB_page *mp = mc->mc_pg[i];
 
-                       if (mc->mc_flags & C_SUB)
-                               dbi--;
-
                        for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
                                if (mc->mc_flags & C_SUB)
                                        m3 = &m2->mc_xcursor->mx_cursor;
@@ -6219,6 +6234,7 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
 {
        unsigned int     i;
        size_t           node_size = NODESIZE;
+       ssize_t          room;
        indx_t           ofs;
        MDB_node        *node;
        MDB_page        *mp = mc->mc_pg[mc->mc_top];
@@ -6249,9 +6265,9 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
                return MDB_SUCCESS;
        }
 
+       room = (ssize_t)SIZELEFT(mp) - (ssize_t)sizeof(indx_t);
        if (key != NULL)
                node_size += key->mv_size;
-
        if (IS_LEAF(mp)) {
                assert(data);
                if (F_ISSET(flags, F_BIGDATA)) {
@@ -6263,26 +6279,23 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
                        /* Put data on overflow page. */
                        DPRINTF(("data size is %"Z"u, node would be %"Z"u, put data on overflow page",
                            data->mv_size, node_size+data->mv_size));
-                       node_size += sizeof(pgno_t);
+                       node_size += sizeof(pgno_t) + (node_size & 1);
+                       if ((ssize_t)node_size > room)
+                               goto full;
                        if ((rc = mdb_page_new(mc, P_OVERFLOW, ovpages, &ofp)))
                                return rc;
                        DPRINTF(("allocated overflow page %"Z"u", ofp->mp_pgno));
                        flags |= F_BIGDATA;
+                       goto update;
                } else {
                        node_size += data->mv_size;
                }
        }
        node_size += node_size & 1;
+       if ((ssize_t)node_size > room)
+               goto full;
 
-       if (node_size + sizeof(indx_t) > SIZELEFT(mp)) {
-               DPRINTF(("not enough room in page %"Z"u, got %u ptrs",
-                   mp->mp_pgno, NUMKEYS(mp)));
-               DPRINTF(("upper - lower = %u - %u = %u", mp->mp_upper, mp->mp_lower,
-                   mp->mp_upper - mp->mp_lower));
-               DPRINTF(("node size = %"Z"u", node_size));
-               return MDB_PAGE_FULL;
-       }
-
+update:
        /* Move higher pointers up one slot. */
        for (i = NUMKEYS(mp); i > indx; i--)
                mp->mp_ptrs[i] = mp->mp_ptrs[i - 1];
@@ -6328,6 +6341,13 @@ mdb_node_add(MDB_cursor *mc, indx_t indx,
        }
 
        return MDB_SUCCESS;
+
+full:
+       DPRINTF(("not enough room in page %"Z"u, got %u ptrs",
+               mp->mp_pgno, NUMKEYS(mp)));
+       DPRINTF(("upper-lower = %u - %u = %"Z"d", mp->mp_upper,mp->mp_lower,room));
+       DPRINTF(("node size = %"Z"u", node_size));
+       return MDB_PAGE_FULL;
 }
 
 /** Delete the specified node from a page.
@@ -6462,11 +6482,13 @@ mdb_xcursor_init0(MDB_cursor *mc)
        mx->mx_cursor.mc_txn = mc->mc_txn;
        mx->mx_cursor.mc_db = &mx->mx_db;
        mx->mx_cursor.mc_dbx = &mx->mx_dbx;
-       mx->mx_cursor.mc_dbi = mc->mc_dbi+1;
+       mx->mx_cursor.mc_dbi = mc->mc_dbi;
        mx->mx_cursor.mc_dbflag = &mx->mx_dbflag;
        mx->mx_cursor.mc_snum = 0;
        mx->mx_cursor.mc_top = 0;
        mx->mx_cursor.mc_flags = C_SUB;
+       mx->mx_dbx.md_name.mv_size = 0;
+       mx->mx_dbx.md_name.mv_data = NULL;
        mx->mx_dbx.md_cmp = mc->mc_dbx->md_dcmp;
        mx->mx_dbx.md_dcmp = NULL;
        mx->mx_dbx.md_rel = mc->mc_dbx->md_rel;
@@ -6487,6 +6509,7 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
                memcpy(&mx->mx_db, NODEDATA(node), sizeof(MDB_db));
                mx->mx_cursor.mc_pg[0] = 0;
                mx->mx_cursor.mc_snum = 0;
+               mx->mx_cursor.mc_top = 0;
                mx->mx_cursor.mc_flags = C_SUB;
        } else {
                MDB_page *fp = NODEDATA(node);
@@ -6499,8 +6522,8 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
                mx->mx_db.md_entries = NUMKEYS(fp);
                COPY_PGNO(mx->mx_db.md_root, fp->mp_pgno);
                mx->mx_cursor.mc_snum = 1;
-               mx->mx_cursor.mc_flags = C_INITIALIZED|C_SUB;
                mx->mx_cursor.mc_top = 0;
+               mx->mx_cursor.mc_flags = C_INITIALIZED|C_SUB;
                mx->mx_cursor.mc_pg[0] = fp;
                mx->mx_cursor.mc_ki[0] = 0;
                if (mc->mc_db->md_flags & MDB_DUPFIXED) {
@@ -6510,12 +6533,9 @@ mdb_xcursor_init1(MDB_cursor *mc, MDB_node *node)
                                mx->mx_db.md_flags |= MDB_INTEGERKEY;
                }
        }
-       DPRINTF(("Sub-db %u for db %u root page %"Z"u", mx->mx_cursor.mc_dbi, mc->mc_dbi,
+       DPRINTF(("Sub-db -%u root page %"Z"u", mx->mx_cursor.mc_dbi,
                mx->mx_db.md_root));
-       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;
+       mx->mx_dbflag = DB_VALID|DB_DIRTY; /* DB_DIRTY guides mdb_cursor_touch */
 #if UINT_MAX < SIZE_MAX
        if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(size_t))
 #ifdef MISALIGNED_OK
@@ -6831,9 +6851,6 @@ mdb_node_move(MDB_cursor *csrc, MDB_cursor *cdst)
                MDB_dbi dbi = csrc->mc_dbi;
                MDB_page *mp = csrc->mc_pg[csrc->mc_top];
 
-               if (csrc->mc_flags & C_SUB)
-                       dbi--;
-
                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;
@@ -7008,9 +7025,6 @@ mdb_page_merge(MDB_cursor *csrc, MDB_cursor *cdst)
                MDB_dbi dbi = csrc->mc_dbi;
                MDB_page *mp = cdst->mc_pg[cdst->mc_top];
 
-               if (csrc->mc_flags & C_SUB)
-                       dbi--;
-
                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;
@@ -7105,13 +7119,11 @@ mdb_rebalance(MDB_cursor *mc)
                        /* Adjust cursors pointing to mp */
                        mc->mc_snum = 0;
                        mc->mc_top = 0;
+                       mc->mc_flags &= ~C_INITIALIZED;
                        {
                                MDB_cursor *m2, *m3;
                                MDB_dbi dbi = mc->mc_dbi;
 
-                               if (mc->mc_flags & C_SUB)
-                                       dbi--;
-
                                for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
                                        if (mc->mc_flags & C_SUB)
                                                m3 = &m2->mc_xcursor->mx_cursor;
@@ -7121,6 +7133,7 @@ mdb_rebalance(MDB_cursor *mc)
                                        if (m3->mc_pg[0] == mp) {
                                                m3->mc_snum = 0;
                                                m3->mc_top = 0;
+                                               m3->mc_flags &= ~C_INITIALIZED;
                                        }
                                }
                        }
@@ -7141,9 +7154,6 @@ mdb_rebalance(MDB_cursor *mc)
                                MDB_cursor *m2, *m3;
                                MDB_dbi dbi = mc->mc_dbi;
 
-                               if (mc->mc_flags & C_SUB)
-                                       dbi--;
-
                                for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
                                        if (mc->mc_flags & C_SUB)
                                                m3 = &m2->mc_xcursor->mx_cursor;
@@ -7712,9 +7722,6 @@ done:
                MDB_dbi dbi = mc->mc_dbi;
                int fixup = NUMKEYS(mp);
 
-               if (mc->mc_flags & C_SUB)
-                       dbi--;
-
                for (m2 = mc->mc_txn->mt_cursors[dbi]; m2; m2=m2->mc_next) {
                        if (mc->mc_flags & C_SUB)
                                m3 = &m2->mc_xcursor->mx_cursor;
@@ -8261,7 +8268,7 @@ static int mdb_pid_insert(pid_t *ids, pid_t pid)
                        return -1;
                }
        }
-       
+
        if( val > 0 ) {
                ++cursor;
        }