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];
56 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
57 op->oq_add.rs_e->e_name.bv_val, 0, 0);
61 /* check entry's schema */
62 rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e,
63 NULL, &rs->sr_text, textbuf, textlen );
64 if ( rs->sr_err != LDAP_SUCCESS ) {
65 Debug( LDAP_DEBUG_TRACE,
66 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
67 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
72 subentry = is_entry_subentry( op->oq_add.rs_e );
76 * acquire an ID outside of the operation transaction
77 * to avoid serializing adds.
79 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
80 if( rs->sr_err != 0 ) {
81 Debug( LDAP_DEBUG_TRACE,
82 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
84 rs->sr_err = LDAP_OTHER;
85 rs->sr_text = "internal error";
90 retry: /* transaction retry */
92 /* free parent and reader lock */
93 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
96 rs->sr_err = TXN_ABORT( ltid );
99 op->o_do_not_cache = opinfo.boi_acl_cache;
100 if( rs->sr_err != 0 ) {
101 rs->sr_err = LDAP_OTHER;
102 rs->sr_text = "internal error";
105 ldap_pvt_thread_yield();
106 bdb_trans_backoff( ++num_retries );
109 /* begin transaction */
110 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
111 bdb->bi_db_opflags );
113 if( rs->sr_err != 0 ) {
114 Debug( LDAP_DEBUG_TRACE,
115 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
116 db_strerror(rs->sr_err), rs->sr_err, 0 );
117 rs->sr_err = LDAP_OTHER;
118 rs->sr_text = "internal error";
122 locker = TXN_ID ( ltid );
124 opinfo.boi_bdb = op->o_bd;
125 opinfo.boi_txn = ltid;
126 opinfo.boi_locker = locker;
128 opinfo.boi_acl_cache = op->o_do_not_cache;
129 op->o_private = &opinfo;
132 * Get the parent dn and see if the corresponding entry exists.
134 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
137 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
140 /* get entry or parent */
141 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
143 switch( rs->sr_err ) {
145 rs->sr_err = LDAP_ALREADY_EXISTS;
149 case DB_LOCK_DEADLOCK:
150 case DB_LOCK_NOTGRANTED:
153 rs->sr_text = "ldap server busy";
156 rs->sr_err = LDAP_OTHER;
157 rs->sr_text = "internal error";
163 if ( !bvmatch( &pdn, &p->e_nname ) ) {
164 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
166 rs->sr_ref = is_entry_referral( p )
167 ? get_entry_referrals( op, p )
169 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
171 Debug( LDAP_DEBUG_TRACE,
172 LDAP_XSTRING(bdb_add) ": parent "
173 "does not exist\n", 0, 0, 0 );
175 rs->sr_err = LDAP_REFERRAL;
176 send_ldap_result( op, rs );
178 ber_bvarray_free( rs->sr_ref );
179 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
181 rs->sr_matched = NULL;
186 rs->sr_err = access_allowed( op, p,
187 children, NULL, ACL_WRITE, NULL );
189 if ( ! rs->sr_err ) {
190 switch( opinfo.boi_err ) {
191 case DB_LOCK_DEADLOCK:
192 case DB_LOCK_NOTGRANTED:
196 Debug( LDAP_DEBUG_TRACE,
197 LDAP_XSTRING(bdb_add) ": no write access "
198 "to parent\n", 0, 0, 0 );
199 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
200 rs->sr_text = "no write access to parent";
201 goto return_results;;
204 #ifdef BDB_SUBENTRIES
205 if ( is_entry_subentry( p ) ) {
206 /* parent is a subentry, don't allow add */
207 Debug( LDAP_DEBUG_TRACE,
208 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
210 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
211 rs->sr_text = "parent is a subentry";
212 goto return_results;;
215 if ( is_entry_alias( p ) ) {
216 /* parent is an alias, don't allow add */
217 Debug( LDAP_DEBUG_TRACE,
218 LDAP_XSTRING(bdb_add) ": parent is alias\n",
220 rs->sr_err = LDAP_ALIAS_PROBLEM;
221 rs->sr_text = "parent is an alias";
222 goto return_results;;
225 if ( is_entry_referral( p ) ) {
226 /* parent is a referral, don't allow add */
227 rs->sr_matched = p->e_name.bv_val;
228 rs->sr_ref = get_entry_referrals( op, p );
230 Debug( LDAP_DEBUG_TRACE,
231 LDAP_XSTRING(bdb_add) ": parent is referral\n",
234 rs->sr_err = LDAP_REFERRAL;
235 send_ldap_result( op, rs );
237 ber_bvarray_free( rs->sr_ref );
238 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
240 rs->sr_matched = NULL;
245 #ifdef BDB_SUBENTRIES
248 /* parent must be an administrative point of the required kind */
252 /* free parent and reader lock */
253 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
259 * if not attempting to add entry at suffix or with parent ""
261 if ((( !be_isroot( op ) && !be_shadow_update(op) )
262 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
264 Debug( LDAP_DEBUG_TRACE,
265 LDAP_XSTRING(bdb_add) ": %s denied\n",
266 pdn.bv_len == 0 ? "suffix" : "entry at root",
268 rs->sr_err = LDAP_NO_SUCH_OBJECT;
273 if ( get_assert( op ) &&
274 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
275 != LDAP_COMPARE_TRUE ))
277 rs->sr_err = LDAP_ASSERTION_FAILED;
281 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
282 entry, NULL, ACL_WRITE, NULL );
284 if ( ! rs->sr_err ) {
285 switch( opinfo.boi_err ) {
286 case DB_LOCK_DEADLOCK:
287 case DB_LOCK_NOTGRANTED:
291 Debug( LDAP_DEBUG_TRACE,
292 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
294 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
295 rs->sr_text = "no write access to entry";
296 goto return_results;;
299 /* nested transaction */
300 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
301 bdb->bi_db_opflags );
303 if( rs->sr_err != 0 ) {
304 Debug( LDAP_DEBUG_TRACE,
305 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
306 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
307 rs->sr_err = LDAP_OTHER;
308 rs->sr_text = "internal error";
313 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
314 if ( rs->sr_err != 0 ) {
315 Debug( LDAP_DEBUG_TRACE,
316 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
317 db_strerror(rs->sr_err), rs->sr_err, 0 );
319 switch( rs->sr_err ) {
320 case DB_LOCK_DEADLOCK:
321 case DB_LOCK_NOTGRANTED:
324 rs->sr_err = LDAP_ALREADY_EXISTS;
327 rs->sr_err = LDAP_OTHER;
333 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
334 if ( rs->sr_err != 0 ) {
335 Debug( LDAP_DEBUG_TRACE,
336 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
338 switch( rs->sr_err ) {
339 case DB_LOCK_DEADLOCK:
340 case DB_LOCK_NOTGRANTED:
343 rs->sr_err = LDAP_OTHER;
345 rs->sr_text = "entry store failed";
349 /* attribute indexes */
350 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
351 if ( rs->sr_err != LDAP_SUCCESS ) {
352 Debug( LDAP_DEBUG_TRACE,
353 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
355 switch( rs->sr_err ) {
356 case DB_LOCK_DEADLOCK:
357 case DB_LOCK_NOTGRANTED:
360 rs->sr_err = LDAP_OTHER;
362 rs->sr_text = "index generation failed";
365 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
366 rs->sr_err = LDAP_OTHER;
367 rs->sr_text = "txn_commit(2) failed";
371 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
372 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei,
373 &ctxcsn_e, &ctxcsn_added, locker );
383 if( op->o_postread ) {
384 if( postread_ctrl == NULL ) {
385 postread_ctrl = &ctrls[num_ctrls++];
386 ctrls[num_ctrls] = NULL;
388 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
389 &slap_post_read_bv, postread_ctrl ) )
391 Debug( LDAP_DEBUG_TRACE,
392 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
393 "failed!\n", 0, 0, 0 );
399 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
400 rs->sr_text = "txn_abort (no-op) failed";
402 rs->sr_err = LDAP_NO_OPERATION;
408 Entry *e = entry_dup( op->ora_e );
411 nrdn.bv_val = e->e_nname.bv_val;
412 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
417 bdb_cache_add( bdb, ei, e, &nrdn, locker );
419 if ( suffix_ei == NULL ) {
423 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
424 if ( ctxcsn_added ) {
425 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
426 (struct berval *)&slap_ldapsync_cn_bv, locker );
430 if ( rs->sr_err == LDAP_SUCCESS && !op->o_no_psearch ) {
431 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
433 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
434 rc = bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_ADD );
436 Debug( LDAP_DEBUG_TRACE,
437 LDAP_XSTRING(bdb_add)
438 ": persistent search failed "
443 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
446 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
447 rs->sr_text = "txn_commit failed";
449 rs->sr_err = LDAP_SUCCESS;
454 op->o_private = NULL;
456 if ( rs->sr_err != LDAP_SUCCESS ) {
457 Debug( LDAP_DEBUG_TRACE,
458 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
459 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
460 rs->sr_err = LDAP_OTHER;
464 Debug(LDAP_DEBUG_TRACE,
465 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
466 op->o_noop ? " (no-op)" : "",
467 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
470 if( num_ctrls ) rs->sr_ctrls = ctrls;
473 send_ldap_result( op, rs );
475 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
476 ldap_pvt_thread_yield();
477 TXN_CHECKPOINT( bdb->bi_dbenv,
478 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
484 op->o_private = NULL;
487 if( postread_ctrl != NULL ) {
488 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
489 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );