1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2004 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
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>.
20 #include <ac/string.h>
26 bdb_add(Operation *op, SlapReply *rs )
28 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
32 char textbuf[SLAP_TEXT_BUFLEN];
33 size_t textlen = sizeof textbuf;
34 AttributeDescription *children = slap_schema.si_ad_children;
35 AttributeDescription *entry = slap_schema.si_ad_entry;
36 DB_TXN *ltid = NULL, *lt2;
37 struct bdb_op_info opinfo;
48 EntryInfo *suffix_ei = NULL;
52 LDAPControl **postread_ctrl = NULL;
53 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
57 LDAP_LOG ( OPERATION, ARGS, "==> bdb_add: %s\n",
58 op->oq_add.rs_e->e_name.bv_val, 0, 0 );
60 Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n",
61 op->oq_add.rs_e->e_name.bv_val, 0, 0);
66 /* check entry's schema */
67 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
68 NULL, &rs->sr_text, textbuf, textlen );
69 if ( rs->sr_err != LDAP_SUCCESS ) {
71 LDAP_LOG ( OPERATION, ERR,
72 "bdb_add: entry failed schema check: %s (%d)\n",
73 rs->sr_text, rs->sr_err, 0 );
75 Debug( LDAP_DEBUG_TRACE,
76 "bdb_add: entry failed schema check: %s (%d)\n",
77 rs->sr_text, rs->sr_err, 0 );
83 subentry = is_entry_subentry( op->oq_add.rs_e );
87 * acquire an ID outside of the operation transaction
88 * to avoid serializing adds.
90 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
91 if( rs->sr_err != 0 ) {
93 LDAP_LOG ( OPERATION, ERR,
94 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
96 Debug( LDAP_DEBUG_TRACE,
97 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
99 rs->sr_err = LDAP_OTHER;
100 rs->sr_text = "internal error";
105 retry: /* transaction retry */
107 /* free parent and reader lock */
108 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
111 rs->sr_err = TXN_ABORT( ltid );
113 op->o_private = NULL;
114 op->o_do_not_cache = opinfo.boi_acl_cache;
115 if( rs->sr_err != 0 ) {
116 rs->sr_err = LDAP_OTHER;
117 rs->sr_text = "internal error";
120 ldap_pvt_thread_yield();
121 bdb_trans_backoff( ++num_retries );
124 /* begin transaction */
125 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
126 bdb->bi_db_opflags );
128 if( rs->sr_err != 0 ) {
130 LDAP_LOG ( OPERATION, ERR,
131 "bdb_add: txn_begin failed: %s (%d)\n",
132 db_strerror(rs->sr_err), rs->sr_err, 0 );
134 Debug( LDAP_DEBUG_TRACE,
135 "bdb_add: txn_begin failed: %s (%d)\n",
136 db_strerror(rs->sr_err), rs->sr_err, 0 );
138 rs->sr_err = LDAP_OTHER;
139 rs->sr_text = "internal error";
143 locker = TXN_ID ( ltid );
145 opinfo.boi_bdb = op->o_bd;
146 opinfo.boi_txn = ltid;
147 opinfo.boi_locker = locker;
149 opinfo.boi_acl_cache = op->o_do_not_cache;
150 op->o_private = &opinfo;
153 * Get the parent dn and see if the corresponding entry exists.
155 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
158 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
161 /* get entry or parent */
162 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
164 switch( rs->sr_err ) {
166 rs->sr_err = LDAP_ALREADY_EXISTS;
170 case DB_LOCK_DEADLOCK:
171 case DB_LOCK_NOTGRANTED:
174 rs->sr_text = "ldap server busy";
177 rs->sr_err = LDAP_OTHER;
178 rs->sr_text = "internal error";
184 if ( !bvmatch( &pdn, &p->e_nname ) ) {
185 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
187 rs->sr_ref = is_entry_referral( p )
188 ? get_entry_referrals( op, p )
190 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
193 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: parent does not exist\n",
196 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
200 rs->sr_err = LDAP_REFERRAL;
201 send_ldap_result( op, rs );
203 ber_bvarray_free( rs->sr_ref );
204 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
206 rs->sr_matched = NULL;
211 rs->sr_err = access_allowed( op, p,
212 children, NULL, ACL_WRITE, NULL );
214 if ( ! rs->sr_err ) {
215 switch( opinfo.boi_err ) {
216 case DB_LOCK_DEADLOCK:
217 case DB_LOCK_NOTGRANTED:
222 LDAP_LOG ( OPERATION, DETAIL1,
223 "bdb_add: no write access to parent\n", 0, 0, 0 );
225 Debug( LDAP_DEBUG_TRACE,
226 "bdb_add: no write access to parent\n", 0, 0, 0 );
228 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
229 rs->sr_text = "no write access to parent";
230 goto return_results;;
233 #ifdef BDB_SUBENTRIES
234 if ( is_entry_subentry( p ) ) {
235 /* parent is a subentry, don't allow add */
237 LDAP_LOG ( OPERATION, DETAIL1,
238 "bdb_add: parent is subentry\n", 0, 0, 0 );
240 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
243 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
244 rs->sr_text = "parent is a subentry";
245 goto return_results;;
249 if ( is_entry_alias( p ) ) {
250 /* parent is an alias, don't allow add */
252 LDAP_LOG ( OPERATION, DETAIL1,
253 "bdb_add: parent is alias\n", 0, 0, 0 );
255 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
258 rs->sr_err = LDAP_ALIAS_PROBLEM;
259 rs->sr_text = "parent is an alias";
260 goto return_results;;
264 if ( is_entry_referral( p ) ) {
265 /* parent is a referral, don't allow add */
266 rs->sr_matched = p->e_name.bv_val;
267 rs->sr_ref = get_entry_referrals( op, p );
270 LDAP_LOG ( OPERATION, DETAIL1,
271 "bdb_add: parent is referral\n", 0, 0, 0 );
273 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
277 rs->sr_err = LDAP_REFERRAL;
278 send_ldap_result( op, rs );
280 ber_bvarray_free( rs->sr_ref );
281 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
283 rs->sr_matched = NULL;
288 #ifdef BDB_SUBENTRIES
291 /* parent must be an administrative point of the required kind */
295 /* free parent and reader lock */
296 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
302 * if not attempting to add entry at suffix or with parent ""
304 if ((( !be_isroot( op ) && !be_shadow_update(op) )
305 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
308 LDAP_LOG ( OPERATION, DETAIL1, "bdb_add: %s denied\n",
309 pdn.bv_len == 0 ? "suffix" : "entry at root", 0, 0 );
311 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
312 pdn.bv_len == 0 ? "suffix" : "entry at root",
315 rs->sr_err = LDAP_NO_SUCH_OBJECT;
320 if ( get_assert( op ) &&
321 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
322 != LDAP_COMPARE_TRUE ))
324 rs->sr_err = LDAP_ASSERTION_FAILED;
328 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
329 entry, NULL, ACL_WRITE, NULL );
331 if ( ! rs->sr_err ) {
332 switch( opinfo.boi_err ) {
333 case DB_LOCK_DEADLOCK:
334 case DB_LOCK_NOTGRANTED:
339 LDAP_LOG ( OPERATION, DETAIL1,
340 "bdb_add: no write access to entry\n", 0, 0, 0 );
342 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
345 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
346 rs->sr_text = "no write access to entry";
347 goto return_results;;
350 /* nested transaction */
351 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
352 bdb->bi_db_opflags );
354 if( rs->sr_err != 0 ) {
356 LDAP_LOG ( OPERATION, ERR,
357 "bdb_add: txn_begin(2) failed: %s (%d)\n",
358 db_strerror(rs->sr_err), rs->sr_err, 0 );
360 Debug( LDAP_DEBUG_TRACE,
361 "bdb_add: txn_begin(2) failed: %s (%d)\n",
362 db_strerror(rs->sr_err), rs->sr_err, 0 );
364 rs->sr_err = LDAP_OTHER;
365 rs->sr_text = "internal error";
370 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
371 if ( rs->sr_err != 0 ) {
373 LDAP_LOG ( OPERATION, ERR,
374 "bdb_add: dn2id_add failed: %s (%d)\n",
375 db_strerror(rs->sr_err), rs->sr_err, 0 );
377 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
378 db_strerror(rs->sr_err), rs->sr_err, 0 );
381 switch( rs->sr_err ) {
382 case DB_LOCK_DEADLOCK:
383 case DB_LOCK_NOTGRANTED:
386 rs->sr_err = LDAP_ALREADY_EXISTS;
389 rs->sr_err = LDAP_OTHER;
395 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
396 if ( rs->sr_err != 0 ) {
398 LDAP_LOG ( OPERATION, ERR, "bdb_add: id2entry_add failed\n", 0, 0, 0 );
400 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
403 switch( rs->sr_err ) {
404 case DB_LOCK_DEADLOCK:
405 case DB_LOCK_NOTGRANTED:
408 rs->sr_err = LDAP_OTHER;
410 rs->sr_text = "entry store failed";
414 /* attribute indexes */
415 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
416 if ( rs->sr_err != LDAP_SUCCESS ) {
418 LDAP_LOG ( OPERATION, ERR,
419 "bdb_add: index_entry_add failed\n", 0, 0, 0 );
421 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
424 switch( rs->sr_err ) {
425 case DB_LOCK_DEADLOCK:
426 case DB_LOCK_NOTGRANTED:
429 rs->sr_err = LDAP_OTHER;
431 rs->sr_text = "index generation failed";
434 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
435 rs->sr_err = LDAP_OTHER;
436 rs->sr_text = "txn_commit(2) failed";
440 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
441 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei,
442 &ctxcsn_e, &ctxcsn_added, locker );
452 if( op->o_postread ) {
453 if( postread_ctrl == NULL ) {
454 postread_ctrl = &ctrls[num_ctrls++];
455 ctrls[num_ctrls] = NULL;
457 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
458 &slap_post_read_bv, postread_ctrl ) )
461 LDAP_LOG ( OPERATION, DETAIL1,
462 "<=- bdb_add: post-read failed!\n", 0, 0, 0 );
464 Debug( LDAP_DEBUG_TRACE,
465 "<=- bdb_add: post-read failed!\n", 0, 0, 0 );
472 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
473 rs->sr_text = "txn_abort (no-op) failed";
475 rs->sr_err = LDAP_NO_OPERATION;
481 Entry *e = entry_dup( op->ora_e );
484 nrdn.bv_val = e->e_nname.bv_val;
485 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
490 bdb_cache_add( bdb, ei, e, &nrdn, locker );
492 if ( suffix_ei == NULL ) {
496 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
497 if ( ctxcsn_added ) {
498 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
499 (struct berval *)&slap_ldapsync_cn_bv, locker );
503 if ( rs->sr_err == LDAP_SUCCESS && !op->o_no_psearch ) {
504 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
506 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
507 bdb_psearch( op, rs, ps_list, e,
508 LDAP_PSEARCH_BY_ADD );
510 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
513 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
514 rs->sr_text = "txn_commit failed";
516 rs->sr_err = LDAP_SUCCESS;
521 op->o_private = NULL;
523 if ( rs->sr_err != LDAP_SUCCESS ) {
525 LDAP_LOG ( OPERATION, ERR, "bdb_add: %s : %s (%d)\n",
526 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
528 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
529 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
531 rs->sr_err = LDAP_OTHER;
536 LDAP_LOG ( OPERATION, RESULTS,
537 "bdb_add: added%s id=%08lx dn=\"%s\"\n",
538 op->o_noop ? " (no-op)" : "",
539 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
541 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
542 op->o_noop ? " (no-op)" : "",
543 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
547 if( num_ctrls ) rs->sr_ctrls = ctrls;
550 send_ldap_result( op, rs );
552 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
553 ldap_pvt_thread_yield();
554 TXN_CHECKPOINT( bdb->bi_dbenv,
555 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
561 op->o_private = NULL;
564 if( postread_ctrl != NULL ) {
565 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
566 slap_sl_free( *postread_ctrl, &op->o_tmpmemctx );