Debian Codegraph @

servers/slapd/back-mdb/id2entry.c

raw
1 /* id2entry.c - routines to deal with the id2entry database */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2000-2024 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
15 */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21 #include <ac/errno.h>
22
23 #include "back-mdb.h"
24
25 typedef struct Ecount {
26 ber_len_t len; /* total entry size */
27 ber_len_t dlen; /* contiguous data size */
28 int nattrs;
29 int nvals;
30 int offset;
31 Attribute *multi;
32 } Ecount;
33
34 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
35 Ecount *eh);
36 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data,
37 Ecount *ec);
38 static Entry *mdb_entry_alloc( Operation *op, int nattrs, int nvals );
39
40 #define ID2VKSZ (sizeof(ID)+2)
41
42 int
43 mdb_id2v_compare(
44 const MDB_val *usrkey,
45 const MDB_val *curkey
46 )
47 {
48 unsigned short *uv, *cv;
49 ID ui, ci;
50 int rc;
51
52 memcpy(&ui, usrkey->mv_data, sizeof(ID));
53 memcpy(&ci, curkey->mv_data, sizeof(ID));
54 if (ui < ci)
55 return -1;
56 if (ui > ci)
57 return 1;
58 uv = usrkey->mv_data;
59 cv = curkey->mv_data;
60 return uv[sizeof(ID)/2] - cv[sizeof(ID)/2];
61 }
62
63 /* usrkey[0] is the key in DB format, as described at mdb_mval_put.
64 * usrkey[1] is the value we'll actually match against.
65 * usrkey[2] is the attributeDescription for this value.
66 */
67 int
68 mdb_id2v_dupsort(
69 const MDB_val *usrkey,
70 const MDB_val *curkey
71 )
72 {
73 AttributeDescription *ad = usrkey[2].mv_data;
74 struct berval bv1, bv2;
75 int rc, match, olen;
76 unsigned short s;
77 char *ptr;
78
79 ptr = curkey->mv_data + curkey->mv_size - 2;
80 memcpy(&s, ptr, 2);
81 bv2.bv_val = curkey->mv_data;
82 bv2.bv_len = curkey->mv_size - 3;
83 if (s)
84 bv2.bv_len -= (s+1);
85
86 bv1.bv_val = usrkey[1].mv_data;
87 bv1.bv_len = usrkey[1].mv_size;
88
89 if (ad && ad->ad_type->sat_equality) {
90 MatchingRule *mr = ad->ad_type->sat_equality;
91 rc = mr->smr_match(&match, SLAP_MR_EQUALITY
92 | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
93 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
94 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
95 ad->ad_type->sat_syntax, mr, &bv1, &bv2);
96 } else {
97 match = ber_bvcmp(&bv1, &bv2);
98 }
99
100 return match;
101 }
102
103 /* Values are stored as
104 * [normalized-value NUL ] original-value NUL 2-byte-len
105 * The trailing 2-byte-len is zero if there is no normalized value.
106 * Otherwise, it is the length of the original-value.
107 */
108 int mdb_mval_put(Operation *op, MDB_cursor *mc, ID id, Attribute *a)
109 {
110 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
111 MDB_val key, data[3];
112 char *buf;
113 char ivk[ID2VKSZ];
114 unsigned i;
115 unsigned short s;
116 int rc, len;
117
118 memcpy(ivk, &id, sizeof(id));
119 s = mdb->mi_adxs[a->a_desc->ad_index];
120 memcpy(ivk+sizeof(ID), &s, 2);
121 key.mv_data = &ivk;
122 key.mv_size = sizeof(ivk);
123 if ((a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED) || a->a_desc == slap_schema.si_ad_objectClass)
124 data[2].mv_data = NULL;
125 else
126 data[2].mv_data = a->a_desc;
127
128 for (i=0; i<a->a_numvals; i++) {
129 len = a->a_nvals[i].bv_len + 1 + 2;
130 if (a->a_nvals != a->a_vals) {
131 len += a->a_vals[i].bv_len + 1;
132 data[1].mv_data = a->a_nvals[i].bv_val;
133 data[1].mv_size = a->a_nvals[i].bv_len;
134 } else {
135 data[1].mv_data = a->a_vals[i].bv_val;
136 data[1].mv_size = a->a_vals[i].bv_len;
137 }
138 data[0].mv_size = len;
139 buf = op->o_tmpalloc( len, op->o_tmpmemctx );
140 data[0].mv_data = buf;
141 memcpy(buf, a->a_nvals[i].bv_val, a->a_nvals[i].bv_len);
142 buf += a->a_nvals[i].bv_len;
143 *buf++ = 0;
144 if (a->a_nvals != a->a_vals) {
145 s = a->a_vals[i].bv_len;
146 memcpy(buf, a->a_vals[i].bv_val, a->a_vals[i].bv_len);
147 buf += a->a_vals[i].bv_len;
148 *buf++ = 0;
149 memcpy(buf, &s, 2);
150 } else {
151 *buf++ = 0;
152 *buf++ = 0;
153 }
154 rc = mdb_cursor_put(mc, &key, data, 0);
155 op->o_tmpfree( data[0].mv_data, op->o_tmpmemctx );
156 if (rc)
157 return rc;
158 }
159 return 0;
160 }
161
162 int mdb_mval_del(Operation *op, MDB_cursor *mc, ID id, Attribute *a)
163 {
164 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
165 MDB_val key, data[3];
166 char *ptr;
167 char ivk[ID2VKSZ];
168 unsigned i;
169 int rc;
170 unsigned short s;
171
172 memcpy(ivk, &id, sizeof(id));
173 s = mdb->mi_adxs[a->a_desc->ad_index];
174 memcpy(ivk+sizeof(ID), &s, 2);
175 key.mv_data = &ivk;
176 key.mv_size = sizeof(ivk);
177 if ((a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED) || a->a_desc == slap_schema.si_ad_objectClass)
178 data[2].mv_data = NULL;
179 else
180 data[2].mv_data = a->a_desc;
181
182 if (a->a_numvals) {
183 for (i=0; i<a->a_numvals; i++) {
184 data[0].mv_data = a->a_nvals[i].bv_val;
185 data[0].mv_size = a->a_nvals[i].bv_len+1;
186 if (a->a_nvals != a->a_vals) {
187 data[1].mv_data = a->a_nvals[i].bv_val;
188 data[1].mv_size = a->a_nvals[i].bv_len;
189 } else {
190 data[1].mv_data = a->a_vals[i].bv_val;
191 data[1].mv_size = a->a_vals[i].bv_len;
192 }
193 rc = mdb_cursor_get(mc, &key, data, MDB_GET_BOTH_RANGE);
194 if (rc)
195 return rc;
196 rc = mdb_cursor_del(mc, 0);
197 if (rc)
198 return rc;
199 }
200 } else {
201 rc = mdb_cursor_get(mc, &key, data, MDB_SET);
202 if (rc)
203 return rc;
204 rc = mdb_cursor_del(mc, MDB_NODUPDATA);
205 }
206 return rc;
207 }
208
209 static int mdb_mval_get(Operation *op, MDB_cursor *mc, ID id, Attribute *a, int have_nvals)
210 {
211 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
212 MDB_val key, data[3];
213 char *ptr;
214 char ivk[ID2VKSZ];
215 unsigned i;
216 int rc = 0;
217 unsigned short s;
218
219 memcpy(ivk, &id, sizeof(id));
220 s = mdb->mi_adxs[a->a_desc->ad_index];
221 memcpy(ivk+sizeof(ID), &s, 2);
222 key.mv_data = &ivk;
223 key.mv_size = sizeof(ivk);
224
225 /* not needed */
226 if ((a->a_desc->ad_type->sat_flags & SLAP_AT_ORDERED) || a->a_desc == slap_schema.si_ad_objectClass)
227 data[2].mv_data = NULL;
228 else
229 data[2].mv_data = a->a_desc;
230
231 if (have_nvals)
232 a->a_nvals = a->a_vals + a->a_numvals + 1;
233 else
234 a->a_nvals = a->a_vals;
235 for (i=0; i<a->a_numvals; i++) {
236 if (!i)
237 rc = mdb_cursor_get(mc, &key, data, MDB_SET);
238 else
239 rc = mdb_cursor_get(mc, &key, data, MDB_NEXT_DUP);
240 if (rc)
241 break;
242 ptr = (char*)data[0].mv_data + data[0].mv_size - 2;
243 memcpy(&s, ptr, 2);
244 if (have_nvals) {
245 a->a_nvals[i].bv_val = data[0].mv_data;
246 a->a_vals[i].bv_len = s;
247 a->a_vals[i].bv_val = ptr - a->a_vals[i].bv_len - 1;
248 a->a_nvals[i].bv_len = a->a_vals[i].bv_val - a->a_nvals[i].bv_val - 1;
249 } else {
250 assert(!s);
251 a->a_vals[i].bv_val = data[0].mv_data;
252 a->a_vals[i].bv_len = data[0].mv_size - 3;
253 }
254 }
255 a->a_numvals = i;
256 BER_BVZERO(&a->a_vals[i]);
257 if (have_nvals) {
258 BER_BVZERO(&a->a_nvals[i]);
259 }
260 return rc;
261 }
262
263 #define ADD_FLAGS (MDB_NOOVERWRITE|MDB_APPEND)
264
265 static int mdb_id2entry_put(
266 Operation *op,
267 MDB_txn *txn,
268 MDB_cursor *mc,
269 Entry *e,
270 int flag )
271 {
272 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
273 Ecount ec;
274 MDB_val key, data;
275 int rc, adding = flag, prev_ads = mdb->mi_numads;
276
277 /* We only store rdns, and they go in the dn2id database. */
278
279 key.mv_data = &e->e_id;
280 key.mv_size = sizeof(ID);
281
282 rc = mdb_entry_partsize( mdb, txn, e, &ec );
283 if (rc) {
284 rc = LDAP_OTHER;
285 goto fail;
286 }
287
288 flag |= MDB_RESERVE;
289
290 if (e->e_id < mdb->mi_nextid)
291 flag &= ~MDB_APPEND;
292
293 if (mdb->mi_maxentrysize && ec.len > mdb->mi_maxentrysize) {
294 rc = LDAP_ADMINLIMIT_EXCEEDED;
295 goto fail;
296 }
297
298 again:
299 data.mv_size = ec.dlen;
300 if ( mc )
301 rc = mdb_cursor_put( mc, &key, &data, flag );
302 else
303 rc = mdb_put( txn, mdb->mi_id2entry, &key, &data, flag );
304 if (rc == MDB_SUCCESS) {
305 rc = mdb_entry_encode( op, e, &data, &ec );
306 if( rc != LDAP_SUCCESS )
307 goto fail;
308 /* Handle adds of large multi-valued attrs here.
309 * Modifies handle them directly.
310 */
311 if (adding && ec.multi) {
312 MDB_cursor *mvc;
313 Attribute *a;
314 rc = mdb_cursor_open( txn, mdb->mi_dbis[MDB_ID2VAL], &mvc );
315 if( !rc ) {
316 for ( a = ec.multi; a; a=a->a_next ) {
317 if (!(a->a_flags & SLAP_ATTR_BIG_MULTI))
318 continue;
319 rc = mdb_mval_put( op, mvc, e->e_id, a );
320 if( rc )
321 break;
322 }
323 mdb_cursor_close( mvc );
324 }
325 if ( rc ) {
326 Debug( LDAP_DEBUG_ANY,
327 "mdb_id2entry_put: mdb_mval_put failed: %s(%d) \"%s\"\n",
328 mdb_strerror(rc), rc,
329 e->e_nname.bv_val );
330 rc = LDAP_OTHER;
331 goto fail;
332 }
333 }
334 }
335 if (rc) {
336 /* Was there a hole from slapadd? */
337 if ( (flag & MDB_NOOVERWRITE) && data.mv_size == 0 ) {
338 flag &= ~ADD_FLAGS;
339 goto again;
340 }
341 Debug( LDAP_DEBUG_ANY,
342 "mdb_id2entry_put: mdb_put failed: %s(%d) \"%s\"\n",
343 mdb_strerror(rc), rc,
344 e->e_nname.bv_val );
345 if ( rc != MDB_KEYEXIST )
346 rc = LDAP_OTHER;
347 }
348 fail:
349 if (rc) {
350 mdb_ad_unwind( mdb, prev_ads );
351 }
352 return rc;
353 }
354
355 /*
356 * This routine adds (or updates) an entry on disk.
357 */
358 int mdb_id2entry_add(
359 Operation *op,
360 MDB_txn *txn,
361 MDB_cursor *mc,
362 Entry *e )
363 {
364 return mdb_id2entry_put(op, txn, mc, e, ADD_FLAGS);
365 }
366
367 int mdb_id2entry_update(
368 Operation *op,
369 MDB_txn *txn,
370 MDB_cursor *mc,
371 Entry *e )
372 {
373 return mdb_id2entry_put(op, txn, mc, e, 0);
374 }
375
376 int mdb_id2edata(
377 Operation *op,
378 MDB_cursor *mc,
379 ID id,
380 MDB_val *data )
381 {
382 MDB_val key;
383 int rc;
384
385 key.mv_data = &id;
386 key.mv_size = sizeof(ID);
387
388 /* fetch it */
389 rc = mdb_cursor_get( mc, &key, data, MDB_SET );
390 /* stubs from missing parents - DB is actually invalid */
391 if ( rc == MDB_SUCCESS && !data->mv_size )
392 rc = MDB_NOTFOUND;
393 return rc;
394 }
395
396 int mdb_id2entry(
397 Operation *op,
398 MDB_cursor *mc,
399 ID id,
400 Entry **e )
401 {
402 MDB_val key, data;
403 int rc = 0;
404
405 *e = NULL;
406
407 key.mv_data = &id;
408 key.mv_size = sizeof(ID);
409
410 /* fetch it */
411 rc = mdb_cursor_get( mc, &key, &data, MDB_SET );
412 if ( rc == MDB_NOTFOUND ) {
413 /* Looking for root entry on an empty-dn suffix? */
414 if ( !id && BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] )) {
415 struct berval gluebv = BER_BVC("glue");
416 Entry *r = mdb_entry_alloc(op, 2, 4);
417 Attribute *a = r->e_attrs;
418 struct berval *bptr;
419
420 r->e_id = 0;
421 r->e_ocflags = SLAP_OC_GLUE|SLAP_OC__END;
422 bptr = a->a_vals;
423 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
424 a->a_desc = slap_schema.si_ad_objectClass;
425 a->a_nvals = a->a_vals;
426 a->a_numvals = 1;
427 *bptr++ = gluebv;
428 BER_BVZERO(bptr);
429 bptr++;
430 a->a_next = a+1;
431 a = a->a_next;
432 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
433 a->a_desc = slap_schema.si_ad_structuralObjectClass;
434 a->a_vals = bptr;
435 a->a_nvals = a->a_vals;
436 a->a_numvals = 1;
437 *bptr++ = gluebv;
438 BER_BVZERO(bptr);
439 a->a_next = NULL;
440 *e = r;
441 return MDB_SUCCESS;
442 }
443 }
444 /* stubs from missing parents - DB is actually invalid */
445 if ( rc == MDB_SUCCESS && !data.mv_size )
446 rc = MDB_NOTFOUND;
447 if ( rc ) return rc;
448
449 rc = mdb_entry_decode( op, mdb_cursor_txn( mc ), &data, id, e );
450 if ( rc ) return rc;
451
452 (*e)->e_id = id;
453 (*e)->e_name.bv_val = NULL;
454 (*e)->e_nname.bv_val = NULL;
455
456 return rc;
457 }
458
459 int mdb_id2entry_delete(
460 BackendDB *be,
461 MDB_txn *tid,
462 Entry *e )
463 {
464 struct mdb_info *mdb = (struct mdb_info *) be->be_private;
465 MDB_dbi dbi = mdb->mi_id2entry;
466 MDB_val key;
467 MDB_cursor *mvc;
468 char kbuf[sizeof(ID) + sizeof(unsigned short)];
469 int rc;
470
471 memcpy( kbuf, &e->e_id, sizeof(ID) );
472 memset( kbuf+sizeof(ID), 0, sizeof(unsigned short) );
473 key.mv_data = kbuf;
474 key.mv_size = sizeof(kbuf);
475
476 /* delete from database */
477 rc = mdb_del( tid, dbi, &key, NULL );
478 if (rc)
479 return rc;
480 rc = mdb_cursor_open( tid, mdb->mi_dbis[MDB_ID2VAL], &mvc );
481 if (rc)
482 return rc;
483
484 rc = mdb_cursor_get( mvc, &key, NULL, MDB_SET_RANGE );
485 if (rc) {
486 if (rc == MDB_NOTFOUND)
487 rc = MDB_SUCCESS;
488 return rc;
489 }
490 while (*(ID *)key.mv_data == e->e_id ) {
491 rc = mdb_cursor_del( mvc, MDB_NODUPDATA );
492 if (rc)
493 return rc;
494 rc = mdb_cursor_get( mvc, &key, NULL, MDB_GET_CURRENT );
495 if (rc) {
496 /* no record or DB is empty */
497 if (rc == MDB_NOTFOUND || rc == EINVAL)
498 rc = MDB_SUCCESS;
499 break;
500 }
501 }
502 return rc;
503 }
504
505 static Entry * mdb_entry_alloc(
506 Operation *op,
507 int nattrs,
508 int nvals )
509 {
510 Entry *e = op->o_tmpalloc( sizeof(Entry) +
511 nattrs * sizeof(Attribute) +
512 nvals * sizeof(struct berval), op->o_tmpmemctx );
513 BER_BVZERO(&e->e_bv);
514 e->e_private = e;
515 if (nattrs) {
516 e->e_attrs = (Attribute *)(e+1);
517 e->e_attrs->a_vals = (struct berval *)(e->e_attrs+nattrs);
518 } else {
519 e->e_attrs = NULL;
520 }
521
522 return e;
523 }
524
525 int mdb_entry_return(
526 Operation *op,
527 Entry *e
528 )
529 {
530 if ( !e )
531 return 0;
532 if ( e->e_private ) {
533 if ( op->o_hdr && op->o_tmpmfuncs ) {
534 op->o_tmpfree( e->e_nname.bv_val, op->o_tmpmemctx );
535 op->o_tmpfree( e->e_name.bv_val, op->o_tmpmemctx );
536 op->o_tmpfree( e, op->o_tmpmemctx );
537 } else {
538 ch_free( e->e_nname.bv_val );
539 ch_free( e->e_name.bv_val );
540 ch_free( e );
541 }
542 } else {
543 entry_free( e );
544 }
545 return 0;
546 }
547
548 int mdb_entry_release(
549 Operation *op,
550 Entry *e,
551 int rw )
552 {
553 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
554 struct mdb_op_info *moi = NULL;
555 int release = 1;
556
557 /* slapMode : SLAP_SERVER_MODE, SLAP_TOOL_MODE,
558 SLAP_TRUNCATE_MODE, SLAP_UNDEFINED_MODE */
559
560 if ( slapMode & SLAP_SERVER_MODE ) {
561 OpExtra *oex;
562
563 /* Only Add ops call with rw set, and in that case the entry
564 * was not created by the backend. So always just release it.
565 *
566 * Otherwise, the entry was read from a backend, and we need
567 * to be sure it was read from this backend, otherwise leave
568 * it alone for someone else to release.
569 */
570 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
571 if ( oex->oe_key == mdb ) {
572 moi = (mdb_op_info *)oex;
573 /* If it was setup by entry_get we should probably free it */
574 if (( moi->moi_flag & (MOI_FREEIT|MOI_KEEPER)) == MOI_FREEIT ) {
575 moi->moi_ref--;
576 if ( moi->moi_ref < 1 ) {
577 mdb_txn_reset( moi->moi_txn );
578 moi->moi_ref = 0;
579 LDAP_SLIST_REMOVE( &op->o_extra, &moi->moi_oe, OpExtra, oe_next );
580 op->o_tmpfree( moi, op->o_tmpmemctx );
581 }
582 }
583 break;
584 }
585 }
586 /* If read, other backends were in use, and not ours, don't release */
587 if ( !rw && ( LDAP_SLIST_FIRST( &op->o_extra ) && !oex ))
588 release = 0;
589 }
590
591 if (release)
592 mdb_entry_return( op, e );
593
594 return release ? 0 : SLAP_CB_CONTINUE;
595 }
596
597 /* return LDAP_SUCCESS IFF we can retrieve the specified entry.
598 */
599 int mdb_entry_get(
600 Operation *op,
601 struct berval *ndn,
602 ObjectClass *oc,
603 AttributeDescription *at,
604 int rw,
605 Entry **ent )
606 {
607 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
608 struct mdb_op_info *moi = NULL;
609 MDB_txn *txn = NULL;
610 Entry *e = NULL;
611 int rc;
612 const char *at_name = at ? at->ad_cname.bv_val : "(null)";
613
614 Debug( LDAP_DEBUG_ARGS,
615 "=> mdb_entry_get: ndn: \"%s\"\n", ndn->bv_val );
616 Debug( LDAP_DEBUG_ARGS,
617 "=> mdb_entry_get: oc: \"%s\", at: \"%s\"\n",
618 oc ? oc->soc_cname.bv_val : "(null)", at_name );
619
620 rc = mdb_opinfo_get( op, mdb, rw == 0, &moi );
621 if ( rc )
622 return LDAP_OTHER;
623 txn = moi->moi_txn;
624
625 /* can we find entry */
626 rc = mdb_dn2entry( op, txn, NULL, ndn, &e, NULL, 0 );
627 switch( rc ) {
628 case MDB_NOTFOUND:
629 case 0:
630 break;
631 default:
632 return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
633 }
634 if (e == NULL) {
635 Debug( LDAP_DEBUG_ACL,
636 "=> mdb_entry_get: cannot find entry: \"%s\"\n",
637 ndn->bv_val );
638 rc = LDAP_NO_SUCH_OBJECT;
639 goto return_results;
640 }
641
642 Debug( LDAP_DEBUG_ACL,
643 "=> mdb_entry_get: found entry: \"%s\"\n",
644 ndn->bv_val );
645
646 if ( oc && !is_entry_objectclass( e, oc, 0 )) {
647 Debug( LDAP_DEBUG_ACL,
648 "<= mdb_entry_get: failed to find objectClass %s\n",
649 oc->soc_cname.bv_val );
650 rc = LDAP_NO_SUCH_ATTRIBUTE;
651 goto return_results;
652 }
653
654 /* NOTE: attr_find() or attrs_find()? */
655 if ( at && attr_find( e->e_attrs, at ) == NULL ) {
656 Debug( LDAP_DEBUG_ACL,
657 "<= mdb_entry_get: failed to find attribute %s\n",
658 at->ad_cname.bv_val );
659 rc = LDAP_NO_SUCH_ATTRIBUTE;
660 goto return_results;
661 }
662
663 return_results:
664 if( rc != LDAP_SUCCESS ) {
665 /* free entry */
666 mdb_entry_release( op, e, rw );
667 } else {
668 *ent = e;
669 }
670
671 Debug( LDAP_DEBUG_TRACE,
672 "mdb_entry_get: rc=%d\n",
673 rc );
674 return(rc);
675 }
676
677 static void
678 mdb_reader_free( void *key, void *data )
679 {
680 MDB_txn *txn = data;
681
682 if ( txn ) mdb_txn_abort( txn );
683 }
684
685 /* free up any keys used by the main thread */
686 void
687 mdb_reader_flush( MDB_env *env )
688 {
689 void *data;
690 void *ctx = ldap_pvt_thread_pool_context();
691
692 if ( !ldap_pvt_thread_pool_getkey( ctx, env, &data, NULL ) ) {
693 ldap_pvt_thread_pool_setkey( ctx, env, NULL, 0, NULL, NULL );
694 mdb_reader_free( env, data );
695 }
696 }
697
698 extern MDB_txn *mdb_tool_txn;
699
700 int
701 mdb_opinfo_get( Operation *op, struct mdb_info *mdb, int rdonly, mdb_op_info **moip )
702 {
703 int rc, renew = 0;
704 void *data;
705 void *ctx;
706 mdb_op_info *moi = NULL;
707 OpExtra *oex;
708
709 assert( op != NULL );
710
711 if ( !mdb || !moip ) return -1;
712
713 /* If no op was provided, try to find the ctx anyway... */
714 if ( op ) {
715 ctx = op->o_threadctx;
716 } else {
717 ctx = ldap_pvt_thread_pool_context();
718 }
719
720 if ( op ) {
721 LDAP_SLIST_FOREACH( oex, &op->o_extra, oe_next ) {
722 if ( oex->oe_key == mdb ) break;
723 }
724 moi = (mdb_op_info *)oex;
725 }
726
727 if ( !moi ) {
728 moi = *moip;
729
730 if ( !moi ) {
731 if ( op ) {
732 moi = op->o_tmpalloc(sizeof(struct mdb_op_info),op->o_tmpmemctx);
733 } else {
734 moi = ch_malloc(sizeof(mdb_op_info));
735 }
736 moi->moi_flag = MOI_FREEIT;
737 *moip = moi;
738 }
739 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
740 moi->moi_oe.oe_key = mdb;
741 moi->moi_ref = 0;
742 moi->moi_txn = NULL;
743 }
744
745 if ( !rdonly ) {
746 /* This op started as a reader, but now wants to write. */
747 if ( moi->moi_flag & MOI_READER ) {
748 moi = *moip;
749 LDAP_SLIST_INSERT_HEAD( &op->o_extra, &moi->moi_oe, oe_next );
750 } else {
751 /* This op is continuing an existing write txn */
752 *moip = moi;
753 }
754 moi->moi_ref++;
755 if ( !moi->moi_txn ) {
756 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
757 moi->moi_txn = mdb_tool_txn;
758 } else {
759 int flag = 0;
760 #ifdef SLAP_CONTROL_X_LAZY_COMMIT
761 if ( get_lazyCommit( op ))
762 flag |= MDB_NOMETASYNC;
763 #endif
764 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, flag, &moi->moi_txn );
765 if (rc) {
766 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
767 mdb_strerror(rc), rc );
768 }
769 return rc;
770 }
771 }
772 return 0;
773 }
774
775 /* OK, this is a reader */
776 if ( !moi->moi_txn ) {
777 if (( slapMode & SLAP_TOOL_MODE ) && mdb_tool_txn ) {
778 moi->moi_txn = mdb_tool_txn;
779 goto ok;
780 }
781 if ( !ctx ) {
782 /* Shouldn't happen unless we're single-threaded */
783 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
784 if (rc) {
785 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
786 mdb_strerror(rc), rc );
787 }
788 return rc;
789 }
790 if ( ldap_pvt_thread_pool_getkey( ctx, mdb->mi_dbenv, &data, NULL ) ) {
791 int retried = 0;
792 retry:
793 rc = mdb_txn_begin( mdb->mi_dbenv, NULL, MDB_RDONLY, &moi->moi_txn );
794 if (rc == MDB_READERS_FULL && !retried) {
795 int dead;
796 /* if any stale readers were cleared, a slot should be available */
797 if (!mdb_reader_check( mdb->mi_dbenv, &dead ) && dead) {
798 retried = 1;
799 goto retry;
800 }
801 }
802 if (rc) {
803 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: err %s(%d)\n",
804 mdb_strerror(rc), rc );
805 return rc;
806 }
807 data = moi->moi_txn;
808 if ( ( rc = ldap_pvt_thread_pool_setkey( ctx, mdb->mi_dbenv,
809 data, mdb_reader_free, NULL, NULL ) ) ) {
810 mdb_txn_abort( moi->moi_txn );
811 moi->moi_txn = NULL;
812 Debug( LDAP_DEBUG_ANY, "mdb_opinfo_get: thread_pool_setkey failed err (%d)\n",
813 rc );
814 return rc;
815 }
816 } else {
817 moi->moi_txn = data;
818 renew = 1;
819 }
820 moi->moi_flag |= MOI_READER;
821 }
822 ok:
823 if ( moi->moi_ref < 1 ) {
824 moi->moi_ref = 0;
825 }
826 if ( renew ) {
827 rc = mdb_txn_renew( moi->moi_txn );
828 assert(!rc);
829 }
830 moi->moi_ref++;
831 if ( *moip != moi )
832 *moip = moi;
833
834 return 0;
835 }
836
837 int mdb_txn( Operation *op, int txnop, OpExtra **ptr )
838 {
839 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
840 mdb_op_info **moip = (mdb_op_info **)ptr, *moi = *moip;
841 int rc;
842
843 switch( txnop ) {
844 case SLAP_TXN_BEGIN:
845 rc = mdb_opinfo_get( op, mdb, 0, moip );
846 if ( !rc ) {
847 moi = *moip;
848 moi->moi_flag |= MOI_KEEPER;
849 }
850 return rc;
851 case SLAP_TXN_COMMIT:
852 rc = mdb_txn_commit( moi->moi_txn );
853 if ( rc )
854 mdb->mi_numads = 0;
855 op->o_tmpfree( moi, op->o_tmpmemctx );
856 return rc;
857 case SLAP_TXN_ABORT:
858 mdb->mi_numads = 0;
859 mdb_txn_abort( moi->moi_txn );
860 op->o_tmpfree( moi, op->o_tmpmemctx );
861 return 0;
862 }
863 return LDAP_OTHER;
864 }
865
866 /* Count up the sizes of the components of an entry */
867 static int mdb_entry_partsize(struct mdb_info *mdb, MDB_txn *txn, Entry *e,
868 Ecount *eh)
869 {
870 ber_len_t len, dlen;
871 int i, nat = 0, nval = 0, nnval = 0, doff = 0;
872 Attribute *a;
873 unsigned hi;
874
875 eh->multi = NULL;
876 len = 4*sizeof(int); /* nattrs, nvals, ocflags, offset */
877 dlen = len;
878 for (a=e->e_attrs; a; a=a->a_next) {
879 /* For AttributeDesc, we only store the attr index */
880 nat++;
881 if (a->a_desc->ad_index >= MDB_MAXADS) {
882 Debug( LDAP_DEBUG_ANY, "mdb_entry_partsize: too many AttributeDescriptions used\n" );
883 return LDAP_OTHER;
884 }
885 if (!mdb->mi_adxs[a->a_desc->ad_index]) {
886 int rc = mdb_ad_get(mdb, txn, a->a_desc);
887 if (rc)
888 return rc;
889 }
890 len += 2*sizeof(int); /* AD index, numvals */
891 dlen += 2*sizeof(int);
892 nval += a->a_numvals + 1; /* empty berval at end */
893 mdb_attr_multi_thresh( mdb, a->a_desc, &hi, NULL );
894 if (a->a_numvals > hi)
895 a->a_flags |= SLAP_ATTR_BIG_MULTI;
896 if (a->a_flags & SLAP_ATTR_BIG_MULTI)
897 doff += a->a_numvals;
898 for (i=0; i<a->a_numvals; i++) {
899 int alen = a->a_vals[i].bv_len + 1 + sizeof(int); /* len */
900 len += alen;
901 if (a->a_flags & SLAP_ATTR_BIG_MULTI) {
902 if (!eh->multi)
903 eh->multi = a;
904 } else {
905 dlen += alen;
906 }
907 }
908 if (a->a_nvals != a->a_vals) {
909 nval += a->a_numvals + 1;
910 nnval++;
911 if (a->a_flags & SLAP_ATTR_BIG_MULTI)
912 doff += a->a_numvals;
913 for (i=0; i<a->a_numvals; i++) {
914 int alen = a->a_nvals[i].bv_len + 1 + sizeof(int);
915 len += alen;
916 if (!(a->a_flags & SLAP_ATTR_BIG_MULTI))
917 dlen += alen;
918 }
919 }
920 }
921 /* padding */
922 dlen = (dlen + sizeof(ID)-1) & ~(sizeof(ID)-1);
923 eh->len = len;
924 eh->dlen = dlen;
925 eh->nattrs = nat;
926 eh->nvals = nval;
927 eh->offset = nat + nval - nnval - doff;
928 return 0;
929 }
930
931 /* Flag bits for an encoded attribute */
932 #define MDB_AT_SORTED (1U<<(sizeof(unsigned int)*CHAR_BIT-1))
933 /* the values are in sorted order */
934 #define MDB_AT_MULTI (1<<(sizeof(unsigned int)*CHAR_BIT-2))
935 /* the values of this multi-valued attr are stored separately */
936
937 #define MDB_AT_NVALS (1U<<(sizeof(unsigned int)*CHAR_BIT-1))
938 /* this attribute has normalized values */
939
940 /* Flatten an Entry into a buffer. The buffer starts with the count of the
941 * number of attributes in the entry, the total number of values in the
942 * entry, and the e_ocflags. It then contains a list of integers for each
943 * attribute. For each attribute the first integer gives the index of the
944 * matching AttributeDescription, followed by the number of values in the
945 * attribute. If the MDB_AT_SORTED bit of the attr index is set, the
946 * attribute's values are already sorted. If the MDB_AT_MULTI bit of the
947 * attr index is set, the values are stored separately.
948 *
949 * If the MDB_AT_NVALS bit of numvals is set, the attribute also has
950 * normalized values present. (Note - a_numvals is an unsigned int, so this
951 * means it's possible to receive an attribute that we can't encode due
952 * to size overflow. In practice, this should not be an issue.)
953 *
954 * Then the length of each value is listed. If there are normalized values,
955 * their lengths come next. This continues for each attribute. After all
956 * of the lengths for the last attribute, the actual values are copied,
957 * with a NUL terminator after each value.
958 * The buffer is padded to the sizeof(ID). The entire buffer size is
959 * precomputed so that a single malloc can be performed.
960 */
961 static int mdb_entry_encode(Operation *op, Entry *e, MDB_val *data, Ecount *eh)
962 {
963 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
964 ber_len_t i;
965 Attribute *a;
966 unsigned char *ptr;
967 unsigned int *lp, l;
968
969 Debug( LDAP_DEBUG_TRACE, "=> mdb_entry_encode(0x%08lx): %s\n",
970 (long) e->e_id, e->e_dn );
971
972 /* make sure e->e_ocflags is set */
973 if (is_entry_referral(e))
974 ; /* empty */
975
976 lp = (unsigned int *)data->mv_data;
977 *lp++ = eh->nattrs;
978 *lp++ = eh->nvals;
979 *lp++ = (unsigned int)e->e_ocflags;
980 *lp++ = eh->offset;
981 ptr = (unsigned char *)(lp + eh->offset);
982
983 for (a=e->e_attrs; a; a=a->a_next) {
984 if (!a->a_desc->ad_index)
985 return LDAP_UNDEFINED_TYPE;
986 l = mdb->mi_adxs[a->a_desc->ad_index];
987 if (a->a_flags & SLAP_ATTR_BIG_MULTI)
988 l |= MDB_AT_MULTI;
989 if (a->a_flags & SLAP_ATTR_SORTED_VALS)
990 l |= MDB_AT_SORTED;
991 *lp++ = l;
992 l = a->a_numvals;
993 if (a->a_nvals != a->a_vals)
994 l |= MDB_AT_NVALS;
995 *lp++ = l;
996 if (a->a_flags & SLAP_ATTR_BIG_MULTI) {
997 continue;
998 } else {
999 if (a->a_vals) {
1000 for (i=0; a->a_vals[i].bv_val; i++);
1001 assert( i == a->a_numvals );
1002 for (i=0; i<a->a_numvals; i++) {
1003 *lp++ = a->a_vals[i].bv_len;
1004 memcpy(ptr, a->a_vals[i].bv_val,
1005 a->a_vals[i].bv_len);
1006 ptr += a->a_vals[i].bv_len;
1007 *ptr++ = '\0';
1008 }
1009 if (a->a_nvals != a->a_vals) {
1010 for (i=0; i<a->a_numvals; i++) {
1011 *lp++ = a->a_nvals[i].bv_len;
1012 memcpy(ptr, a->a_nvals[i].bv_val,
1013 a->a_nvals[i].bv_len);
1014 ptr += a->a_nvals[i].bv_len;
1015 *ptr++ = '\0';
1016 }
1017 }
1018 }
1019 }
1020 }
1021
1022 Debug( LDAP_DEBUG_TRACE, "<= mdb_entry_encode(0x%08lx): %s\n",
1023 (long) e->e_id, e->e_dn );
1024
1025 return 0;
1026 }
1027
1028 /* Retrieve an Entry that was stored using entry_encode above.
1029 *
1030 * Note: everything is stored in a single contiguous block, so
1031 * you can not free individual attributes or names from this
1032 * structure. Attempting to do so will likely corrupt memory.
1033 */
1034
1035 int mdb_entry_decode(Operation *op, MDB_txn *txn, MDB_val *data, ID id, Entry **e)
1036 {
1037 struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
1038 int i, j, nattrs, nvals;
1039 int rc;
1040 Attribute *a;
1041 Entry *x;
1042 const char *text;
1043 unsigned int *lp = (unsigned int *)data->mv_data;
1044 unsigned char *ptr;
1045 BerVarray bptr;
1046 MDB_cursor *mvc = NULL;
1047
1048 Debug( LDAP_DEBUG_TRACE,
1049 "=> mdb_entry_decode:\n" );
1050
1051 nattrs = *lp++;
1052 nvals = *lp++;
1053 x = mdb_entry_alloc(op, nattrs, nvals);
1054 x->e_ocflags = *lp++;
1055 if (!nvals) {
1056 goto done;
1057 }
1058 a = x->e_attrs;
1059 bptr = a->a_vals;
1060 i = *lp++;
1061 ptr = (unsigned char *)(lp + i);
1062
1063 for (;nattrs>0; nattrs--) {
1064 int have_nval = 0, multi = 0;
1065 a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
1066 i = *lp++;
1067 if (i & MDB_AT_SORTED) {
1068 i ^= MDB_AT_SORTED;
1069 a->a_flags |= SLAP_ATTR_SORTED_VALS;
1070 }
1071 if (i & MDB_AT_MULTI) {
1072 i ^= MDB_AT_MULTI;
1073 a->a_flags |= SLAP_ATTR_BIG_MULTI;
1074 multi = 1;
1075 }
1076 if (i > mdb->mi_numads) {
1077 rc = mdb_ad_read(mdb, txn);
1078 if (rc)
1079 goto leave;
1080 if (i > mdb->mi_numads) {
1081 Debug( LDAP_DEBUG_ANY,
1082 "mdb_entry_decode: attribute index %d not recognized\n",
1083 i );
1084 rc = LDAP_OTHER;
1085 goto leave;
1086 }
1087 }
1088 a->a_desc = mdb->mi_ads[i];
1089 a->a_numvals = *lp++;
1090 if (a->a_numvals & MDB_AT_NVALS) {
1091 a->a_numvals ^= MDB_AT_NVALS;
1092 have_nval = 1;
1093 }
1094 a->a_vals = bptr;
1095 if (multi) {
1096 if (!mvc) {
1097 rc = mdb_cursor_open(txn, mdb->mi_dbis[MDB_ID2VAL], &mvc);
1098 if (rc)
1099 goto leave;
1100 }
1101 i = a->a_numvals;
1102 mdb_mval_get(op, mvc, id, a, have_nval);
1103 bptr += i + 1;
1104 if (have_nval)
1105 bptr += i + 1;
1106 } else {
1107 for (i=0; i<a->a_numvals; i++) {
1108 bptr->bv_len = *lp++;
1109 bptr->bv_val = (char *)ptr;
1110 ptr += bptr->bv_len+1;
1111 bptr++;
1112 }
1113 bptr->bv_val = NULL;
1114 bptr->bv_len = 0;
1115 bptr++;
1116
1117 if (have_nval) {
1118 a->a_nvals = bptr;
1119 for (i=0; i<a->a_numvals; i++) {
1120 bptr->bv_len = *lp++;
1121 bptr->bv_val = (char *)ptr;
1122 ptr += bptr->bv_len+1;
1123 bptr++;
1124 }
1125 bptr->bv_val = NULL;
1126 bptr->bv_len = 0;
1127 bptr++;
1128 } else {
1129 a->a_nvals = a->a_vals;
1130 }
1131 }
1132
1133 /* FIXME: This is redundant once a sorted entry is saved into the DB */
1134 if (( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL )
1135 && !(a->a_flags & SLAP_ATTR_SORTED_VALS)) {
1136 rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
1137 if ( rc == LDAP_SUCCESS ) {
1138 a->a_flags |= SLAP_ATTR_SORTED_VALS;
1139 } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
1140 /* should never happen */
1141 Debug( LDAP_DEBUG_ANY,
1142 "mdb_entry_decode: attributeType %s value #%d provided more than once\n",
1143 a->a_desc->ad_cname.bv_val, j );
1144 goto leave;
1145 }
1146 }
1147 a->a_next = a+1;
1148 a = a->a_next;
1149 }
1150 a[-1].a_next = NULL;
1151 done:
1152 Debug(LDAP_DEBUG_TRACE, "<= mdb_entry_decode\n" );
1153 *e = x;
1154 rc = 0;
1155
1156 leave:
1157 if (mvc)
1158 mdb_cursor_close(mvc);
1159 return rc;
1160 }