Back to index

openldap  2.4.31
id2entry.c
Go to the documentation of this file.
00001 /* id2entry.c - routines to deal with the id2entry database */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2000-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 
00017 #include "portable.h"
00018 
00019 #include <stdio.h>
00020 #include <ac/string.h>
00021 #include <ac/errno.h>
00022 
00023 #include "back-mdb.h"
00024 
00025 typedef struct Ecount {
00026        ber_len_t len;
00027        int nattrs;
00028        int nvals;
00029        int offset;
00030 } Ecount;
00031 
00032 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
00033        Ecount *eh);
00034 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data,
00035        Ecount *ec);
00036 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
00037 
00038 #define ADD_FLAGS    (MDB_NOOVERWRITE|MDB_APPEND)
00039 
00040 static int mdb_id2entry_put(
00041        Operation *op,
00042        MDB_txn *txn,
00043        MDB_cursor *mc,
00044        Entry *e,
00045        int flag )
00046 {
00047        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
00048        Ecount ec;
00049        MDB_val key, data;
00050        int rc;
00051 
00052        /* We only store rdns, and they go in the dn2id database. */
00053 
00054        key.mv_data = &e->e_id;
00055        key.mv_size = sizeof(ID);
00056 
00057        rc = mdb_entry_partsize( mdb, txn, e, &ec );
00058        if (rc)
00059               return LDAP_OTHER;
00060 
00061        flag |= MDB_RESERVE;
00062 
00063 again:
00064        data.mv_size = ec.len;
00065        if ( mc )
00066               rc = mdb_cursor_put( mc, &key, &data, flag );
00067        else
00068               rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
00069        if (rc == MDB_SUCCESS) {
00070               rc = mdb_entry_encode( op, e, &data, &ec );
00071               if( rc != LDAP_SUCCESS )
00072                      return LDAP_OTHER;
00073        }
00074        if (rc) {
00075               /* Was there a hole from slapadd? */
00076               if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
00077                      flag ^= ADD_FLAGS;
00078                      goto again;
00079               }
00080               Debug( LDAP_DEBUG_ANY,
00081                      "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
00082                      mdb_strerror(rc), rc,
00083                      e->e_nname.bv_val );
00084               if ( rc != MDB_KEYEXIST )
00085                      rc = LDAP_OTHER;
00086        }
00087        return rc;
00088 }
00089 
00090 /*
00091  * This routine adds (or updates) an entry on disk.
00092  * The cache should be already be updated.
00093  */
00094 
00095 
00096 int mdb_id2entry_add(
00097        Operation *op,
00098        MDB_txn *txn,
00099        MDB_cursor *mc,
00100        Entry *e )
00101 {
00102        return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
00103 }
00104 
00105 int mdb_id2entry_update(
00106        Operation *op,
00107        MDB_txn *txn,
00108        MDB_cursor *mc,
00109        Entry *e )
00110 {
00111        return mdb_id2entry_put(op, txn, mc, e, 0);
00112 }
00113 
00114 int mdb_id2entry(
00115        Operation *op,
00116        MDB_cursor *mc,
00117        ID id,
00118        Entry **e )
00119 {
00120        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
00121        MDB_val key, data;
00122        int rc = 0;
00123 
00124        *e = NULL;
00125 
00126        key.mv_data = &id;
00127        key.mv_size = sizeof(ID);
00128 
00129        /* fetch it */
00130        rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
00131        if ( rc == MDB_NOTFOUND ) {
00132               /* Looking for root entry on an empty-dn suffix? */
00133               if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
00134                      struct berval gluebv = BER_BVC("glue");
00135                      Entry *r = mdb_entry_alloc(op, 2, 4);
00136                      Attribute *a = r->e_attrs;
00137                      struct berval *bptr;
00138 
00139                      r->e_id = 0;
00140                      r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
00141                      bptr = a->a_vals;
00142                      a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
00143                      a->a_desc = slap_schema.si_ad_objectClass;
00144                      a->a_nvals = a->a_vals;
00145                      a->a_numvals = 1;
00146                      *bptr++ = gluebv;
00147                      BER_BVZERO(bptr);
00148                      bptr++;
00149                      a->a_next = a+1;
00150                      a = a->a_next;
00151                      a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
00152                      a->a_desc = slap_schema.si_ad_structuralObjectClass;
00153                      a->a_vals = bptr;
00154                      a->a_nvals = a->a_vals;
00155                      a->a_numvals = 1;
00156                      *bptr++ = gluebv;
00157                      BER_BVZERO(bptr);
00158                      a->a_next = NULL;
00159                      *e = r;
00160                      return MDB_SUCCESS;
00161               }
00162        }
00163        if ( rc ) return rc;
00164 
00165        rc = mdb_entry_decode( op, &data, e );
00166        if ( rc ) return rc;
00167 
00168        (*e)->e_id = id;
00169        (*e)->e_name.bv_val = NULL;
00170        (*e)->e_nname.bv_val = NULL;
00171 
00172        return rc;
00173 }
00174 
00175 int mdb_id2entry_delete(
00176        BackendDB *be,
00177        MDB_txn *tid,
00178        Entry *e )
00179 {
00180        struct mdb_info *mdb = (struct mdb_info *) be->be_private;
00181        MDB_dbi dbi = mdb->mi_id2entry;
00182        MDB_val key;
00183        int rc;
00184 
00185        key.mv_data = &e->e_id;
00186        key.mv_size = sizeof(ID);
00187 
00188        /* delete from database */
00189        rc = mdb_del( tid, dbi, &key, NULL );
00190 
00191        return rc;
00192 }
00193 
00194 static Entry * mdb_entry_alloc(
00195        Operation *op,
00196        int nattrs,
00197        int nvals )
00198 {
00199        Entry *e = op->o_tmpalloc( sizeof(Entry) +
00200               nattrs * sizeof(Attribute) +
00201               nvals * sizeof(struct berval), op->o_tmpmemctx );
00202        BER_BVZERO(&e->e_bv);
00203        e->e_private = e;
00204        if (nattrs) {
00205               e->e_attrs = (Attribute *)(e+1);
00206               e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
00207        } else {
00208               e->e_attrs = NULL;
00209        }
00210 
00211        return e;
00212 }
00213 
00214 int mdb_entry_return(
00215        Operation *op,
00216        Entry *e
00217 )
00218 {
00219        if ( e->e_private ) {
00220               if ( op->o_hdr ) {
00221                      op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
00222                      op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
00223                      op->o_tmpfree( e, op->o_tmpmemctx );
00224               } else {
00225                      ch_free( e->e_nname.bv_val );
00226                      ch_free( e->e_name.bv_val );
00227                      ch_free( e );
00228               }
00229        } else {
00230               entry_free( e );
00231        }
00232        return 0;
00233 }
00234 
00235 int mdb_entry_release(
00236        Operation *op,
00237        Entry *e,
00238        int rw )
00239 {
00240        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
00241        struct mdb_op_info *moi = NULL;
00242        int rc;
00243  
00244        /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
00245                      SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
00246  
00247        mdb_entry_return( op, e );
00248        if ( slapMode == SLAP_SERVER_MODE ) {
00249               OpExtra *oex;
00250               LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
00251                      if ( oex->oe_key == mdb ) {
00252                             moi = (mdb_op_info *)oex;
00253                             /* If it was setup by entry_get we should probably free it */
00254                             if ( moi->moi_flag & MOI_FREEIT ) {
00255                                    moi->moi_ref--;
00256                                    if ( moi->moi_ref < 1 ) {
00257                                           mdb_txn_reset( moi->moi_txn );
00258                                           moi->moi_ref = 0;
00259                                           LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
00260                                           op->o_tmpfree( moi, op->o_tmpmemctx );
00261                                    }
00262                             }
00263                             break;
00264                      }
00265               }
00266        }
00267  
00268        return 0;
00269 }
00270 
00271 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
00272  */
00273 int mdb_entry_get(
00274        Operation *op,
00275        struct berval *ndn,
00276        ObjectClass *oc,
00277        AttributeDescription *at,
00278        int rw,
00279        Entry **ent )
00280 {
00281        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
00282        struct mdb_op_info *moi = NULL;
00283        MDB_txn *txn = NULL;
00284        Entry *e = NULL;
00285        int    rc;
00286        const char *at_name = at ? at->ad_cname.bv_val : "(null)";
00287 
00288        Debug( LDAP_DEBUG_ARGS,
00289               "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val, 0, 0 ); 
00290        Debug( LDAP_DEBUG_ARGS,
00291               "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
00292               oc ? oc->soc_cname.bv_val : "(null)", at_name, 0);
00293 
00294        rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
00295        if ( rc )
00296               return LDAP_OTHER;
00297        txn = moi->moi_txn;
00298 
00299        /* can we find entry */
00300        rc = mdb_dn2entry( op, txn, NULL, ndn, &e, 0 );
00301        switch( rc ) {
00302        case MDB_NOTFOUND:
00303        case 0:
00304               break;
00305        default:
00306               return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
00307        }
00308        if (e == NULL) {
00309               Debug( LDAP_DEBUG_ACL,
00310                      "=> mdb_entry_get: cannot find entry: \"%s\"\n",
00311                             ndn->bv_val, 0, 0 ); 
00312               rc = LDAP_NO_SUCH_OBJECT;
00313               goto return_results;
00314        }
00315        
00316        Debug( LDAP_DEBUG_ACL,
00317               "=> mdb_entry_get: found entry: \"%s\"\n",
00318               ndn->bv_val, 0, 0 ); 
00319 
00320        if ( oc && !is_entry_objectclass( e, oc, 0 )) {
00321               Debug( LDAP_DEBUG_ACL,
00322                      "<= mdb_entry_get: failed to find objectClass %s\n",
00323                      oc->soc_cname.bv_val, 0, 0 ); 
00324               rc = LDAP_NO_SUCH_ATTRIBUTE;
00325               goto return_results;
00326        }
00327 
00328        /* NOTE: attr_find() or attrs_find()? */
00329        if ( at && attr_find( e->e_attrs, at ) == NULL ) {
00330               Debug( LDAP_DEBUG_ACL,
00331                      "<= mdb_entry_get: failed to find attribute %s\n",
00332                      at->ad_cname.bv_val, 0, 0 ); 
00333               rc = LDAP_NO_SUCH_ATTRIBUTE;
00334               goto return_results;
00335        }
00336 
00337 return_results:
00338        if( rc != LDAP_SUCCESS ) {
00339               /* free entry */
00340               if ( e )
00341                      mdb_entry_return( op, e );
00342 
00343               if (moi->moi_ref == 1) {
00344                      LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
00345                      mdb_txn_reset( txn );
00346                      op->o_tmpfree( moi, op->o_tmpmemctx );
00347               }
00348        } else {
00349               *ent = e;
00350        }
00351 
00352        Debug( LDAP_DEBUG_TRACE,
00353               "mdb_entry_get: rc=%d\n",
00354               rc, 0, 0 ); 
00355        return(rc);
00356 }
00357 
00358 static void
00359 mdb_reader_free( void *key, void *data )
00360 {
00361        MDB_txn *txn = data;
00362 
00363        if ( txn ) mdb_txn_abort( txn );
00364 }
00365 
00366 /* free up any keys used by the main thread */
00367 void
00368 mdb_reader_flush( MDB_env *env )
00369 {
00370        void *data;
00371        void *ctx = ldap_pvt_thread_pool_context();
00372 
00373        if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
00374               ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
00375               mdb_reader_free( env, data );
00376        }
00377 }
00378 
00379 int
00380 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
00381 {
00382        int rc, renew = 0;
00383        void *data;
00384        void *ctx;
00385        mdb_op_info *moi = NULL;
00386        OpExtra *oex;
00387 
00388        assert( op != NULL );
00389 
00390        if ( !mdb || !moip ) return -1;
00391 
00392        /* If no op was provided, try to find the ctx anyway... */
00393        if ( op ) {
00394               ctx = op->o_threadctx;
00395        } else {
00396               ctx = ldap_pvt_thread_pool_context();
00397        }
00398 
00399        if ( op ) {
00400               LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
00401                      if ( oex->oe_key == mdb ) break;
00402               }
00403               moi = (mdb_op_info *)oex;
00404        }
00405 
00406        if ( !moi ) {
00407               moi = *moip;
00408 
00409               if ( !moi ) {
00410                      if ( op ) {
00411                             moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
00412                      } else {
00413                             moi = ch_malloc(sizeof(mdb_op_info));
00414                      }
00415                      moi->moi_flag = MOI_FREEIT;
00416                      *moip = moi;
00417               }
00418               LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
00419               moi->moi_oe.oe_key = mdb;
00420               moi->moi_ref = 0;
00421               moi->moi_txn = NULL;
00422        }
00423 
00424        if ( !rdonly ) {
00425               /* This op started as a reader, but now wants to write. */
00426               if ( moi->moi_flag & MOI_READER ) {
00427                      moi = *moip;
00428                      LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
00429               } else {
00430               /* This op is continuing an existing write txn */
00431                      *moip = moi;
00432               }
00433               moi->moi_ref++;
00434               if ( !moi->moi_txn ) {
00435                      rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &moi->moi_txn );
00436                      if (rc) {
00437                             Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
00438                                    mdb_strerror(rc), rc, 0 );
00439                      }
00440                      return rc;
00441               }
00442               return 0;
00443        }
00444 
00445        /* OK, this is a reader */
00446        if ( !moi->moi_txn ) {
00447               if ( !ctx ) {
00448                      /* Shouldn't happen unless we're single-threaded */
00449                      rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
00450                      if (rc) {
00451                             Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
00452                                    mdb_strerror(rc), rc, 0 );
00453                      }
00454                      return rc;
00455               }
00456               if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
00457                      rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
00458                      if (rc) {
00459                             Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
00460                                    mdb_strerror(rc), rc, 0 );
00461                             return rc;
00462                      }
00463                      data = moi->moi_txn;
00464                      if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
00465                             data, mdb_reader_free, NULL, NULL ) ) ) {
00466                             mdb_txn_abort( moi->moi_txn );
00467                             moi->moi_txn = NULL;
00468                             Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
00469                                    rc, 0, 0 );
00470                             return rc;
00471                      }
00472               } else {
00473                      moi->moi_txn = data;
00474                      renew = 1;
00475               }
00476               moi->moi_flag |= MOI_READER;
00477        }
00478        if ( moi->moi_ref < 1 ) {
00479               moi->moi_ref = 0;
00480        }
00481        if ( renew ) {
00482               mdb_txn_renew( moi->moi_txn );
00483        }
00484        moi->moi_ref++;
00485        if ( *moip != moi )
00486               *moip = moi;
00487 
00488        return 0;
00489 }
00490 
00491 /* Count up the sizes of the components of an entry */
00492 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
00493        Ecount *eh)
00494 {
00495        ber_len_t len;
00496        int i, nat = 0, nval = 0, nnval = 0;
00497        Attribute *a;
00498 
00499        len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
00500        for (a=e->e_attrs; a; a=a->a_next) {
00501               /* For AttributeDesc, we only store the attr index */
00502               nat++;
00503               if (a->a_desc->ad_index >= MDB_MAXADS) {
00504                      Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n",
00505                             0, 0, 0 );
00506                      return LDAP_OTHER;
00507               }
00508               if (!mdb->mi_adxs[a->a_desc->ad_index]) {
00509                      int rc = mdb_ad_get(mdb, txn, a->a_desc);
00510                      if (rc)
00511                             return rc;
00512               }
00513               len += 2*sizeof(int);       /* AD index, numvals */
00514               nval += a->a_numvals + 1;   /* empty berval at end */
00515               for (i=0; i<a->a_numvals; i++) {
00516                      len += a->a_vals[i].bv_len + 1 + sizeof(int);    /* len */
00517               }
00518               if (a->a_nvals != a->a_vals) {
00519                      nval += a->a_numvals + 1;
00520                      nnval++;
00521                      for (i=0; i<a->a_numvals; i++) {
00522                             len += a->a_nvals[i].bv_len + 1 + sizeof(int);;
00523                      }
00524               }
00525        }
00526        /* padding */
00527        len = (len + sizeof(ID)-1) & ~(sizeof(ID)-1);
00528        eh->len = len;
00529        eh->nattrs = nat;
00530        eh->nvals = nval;
00531        eh->offset = nat + nval - nnval;
00532        return 0;
00533 }
00534 
00535 #define HIGH_BIT (1<<(sizeof(unsigned int)*CHAR_BIT-1))
00536 
00537 /* Flatten an Entry into a buffer. The buffer starts with the count of the
00538  * number of attributes in the entry, the total number of values in the
00539  * entry, and the e_ocflags. It then contains a list of integers for each
00540  * attribute. For each attribute the first integer gives the index of the
00541  * matching AttributeDescription, followed by the number of values in the
00542  * attribute. If the high bit is set, the attribute also has normalized
00543  * values present. (Note - a_numvals is an unsigned int, so this means
00544  * it's possible to receive an attribute that we can't encode due to size
00545  * overflow. In practice, this should not be an issue.) Then the length
00546  * of each value is listed. If there are normalized values, their lengths
00547  * come next. This continues for each attribute. After all of the lengths
00548  * for the last attribute, the actual values are copied, with a NUL
00549  * terminator after each value. The buffer is padded to the sizeof(ID).
00550  * The entire buffer size is precomputed so that a single malloc can be
00551  * performed.
00552  */
00553 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
00554 {
00555        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
00556        ber_len_t len, i;
00557        int rc;
00558        Attribute *a;
00559        unsigned char *ptr;
00560        unsigned int *lp, l;
00561 
00562        Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
00563               (long) e->e_id, e->e_dn, 0 );
00564 
00565        /* make sure e->e_ocflags is set */
00566        if (is_entry_referral(e))
00567               ;      /* empty */
00568 
00569        lp = (unsigned int *)data->mv_data;
00570        *lp++ = eh->nattrs;
00571        *lp++ = eh->nvals;
00572        *lp++ = (unsigned int)e->e_ocflags;
00573        *lp++ = eh->offset;
00574        ptr = (unsigned char *)(lp + eh->offset);
00575 
00576        for (a=e->e_attrs; a; a=a->a_next) {
00577               *lp++ = mdb->mi_adxs[a->a_desc->ad_index];
00578               l = a->a_numvals;
00579               if (a->a_nvals != a->a_vals)
00580                      l |= HIGH_BIT;
00581               *lp++ = l;
00582               if (a->a_vals) {
00583                      for (i=0; a->a_vals[i].bv_val; i++);
00584                      assert( i == a->a_numvals );
00585                      for (i=0; i<a->a_numvals; i++) {
00586                             *lp++ = a->a_vals[i].bv_len;
00587                             memcpy(ptr, a->a_vals[i].bv_val,
00588                                    a->a_vals[i].bv_len);
00589                             ptr += a->a_vals[i].bv_len;
00590                             *ptr++ = '\0';
00591                      }
00592                      if (a->a_nvals != a->a_vals) {
00593                             for (i=0; i<a->a_numvals; i++) {
00594                                    *lp++ = a->a_nvals[i].bv_len;
00595                                    memcpy(ptr, a->a_nvals[i].bv_val,
00596                                           a->a_nvals[i].bv_len);
00597                                    ptr += a->a_nvals[i].bv_len;
00598                                    *ptr++ = '\0';
00599                             }
00600                      }
00601               }
00602        }
00603 
00604        Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
00605               (long) e->e_id, e->e_dn, 0 );
00606 
00607        return 0;
00608 }
00609 
00610 /* Retrieve an Entry that was stored using entry_encode above.
00611  *
00612  * Note: everything is stored in a single contiguous block, so
00613  * you can not free individual attributes or names from this
00614  * structure. Attempting to do so will likely corrupt memory.
00615  */
00616 
00617 int mdb_entry_decode(Operation *op, MDB_val *data, Entry **e)
00618 {
00619        struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
00620        int i, j, nattrs, nvals;
00621        int rc;
00622        Attribute *a;
00623        Entry *x;
00624        const char *text;
00625        AttributeDescription *ad;
00626        unsigned int *lp = (unsigned int *)data->mv_data;
00627        unsigned char *ptr;
00628        BerVarray bptr;
00629 
00630        Debug( LDAP_DEBUG_TRACE,
00631               "=> mdb_entry_decode:\n",
00632               0, 0, 0 );
00633 
00634        nattrs = *lp++;
00635        nvals = *lp++;
00636        x = mdb_entry_alloc(op, nattrs, nvals);
00637        x->e_ocflags = *lp++;
00638        if (!nvals) {
00639               goto done;
00640        }
00641        a = x->e_attrs;
00642        bptr = a->a_vals;
00643        i = *lp++;
00644        ptr = (unsigned char *)(lp + i);
00645 
00646        for (;nattrs>0; nattrs--) {
00647               int have_nval = 0;
00648               a->a_desc = mdb->mi_ads[*lp++];
00649               a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
00650               a->a_numvals = *lp++;
00651               if (a->a_numvals & HIGH_BIT) {
00652                      a->a_numvals ^= HIGH_BIT;
00653                      have_nval = 1;
00654               }
00655               a->a_vals = bptr;
00656               for (i=0; i<a->a_numvals; i++) {
00657                      bptr->bv_len = *lp++;;
00658                      bptr->bv_val = (char *)ptr;
00659                      ptr += bptr->bv_len+1;
00660                      bptr++;
00661               }
00662               bptr->bv_val = NULL;
00663               bptr->bv_len = 0;
00664               bptr++;
00665 
00666               if (have_nval) {
00667                      a->a_nvals = bptr;
00668                      for (i=0; i<a->a_numvals; i++) {
00669                             bptr->bv_len = *lp++;
00670                             bptr->bv_val = (char *)ptr;
00671                             ptr += bptr->bv_len+1;
00672                             bptr++;
00673                      }
00674                      bptr->bv_val = NULL;
00675                      bptr->bv_len = 0;
00676                      bptr++;
00677               } else {
00678                      a->a_nvals = a->a_vals;
00679               }
00680               /* FIXME: This is redundant once a sorted entry is saved into the DB */
00681               if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
00682                      rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
00683                      if ( rc == LDAP_SUCCESS ) {
00684                             a->a_flags |= SLAP_ATTR_SORTED_VALS;
00685                      } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
00686                             /* should never happen */
00687                             Debug( LDAP_DEBUG_ANY,
00688                                    "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
00689                                    a->a_desc->ad_cname.bv_val, j, 0 );
00690                             return rc;
00691                      }
00692               }
00693               a->a_next = a+1;
00694               a = a->a_next;
00695        }
00696        a[-1].a_next = NULL;
00697 done:
00698 
00699        Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n",
00700               0, 0, 0 );
00701        *e = x;
00702        return 0;
00703 }