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, "==> 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 "bdb_add: entry failed schema check: %s (%d)\n",
67 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 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
83 rs->sr_err = LDAP_OTHER;
84 rs->sr_text = "internal error";
89 retry: /* transaction retry */
91 /* free parent and reader lock */
92 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
95 rs->sr_err = TXN_ABORT( ltid );
98 op->o_do_not_cache = opinfo.boi_acl_cache;
99 if( rs->sr_err != 0 ) {
100 rs->sr_err = LDAP_OTHER;
101 rs->sr_text = "internal error";
104 ldap_pvt_thread_yield();
105 bdb_trans_backoff( ++num_retries );
108 /* begin transaction */
109 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
110 bdb->bi_db_opflags );
112 if( rs->sr_err != 0 ) {
113 Debug( LDAP_DEBUG_TRACE,
114 "bdb_add: txn_begin failed: %s (%d)\n",
115 db_strerror(rs->sr_err), rs->sr_err, 0 );
116 rs->sr_err = LDAP_OTHER;
117 rs->sr_text = "internal error";
121 locker = TXN_ID ( ltid );
123 opinfo.boi_bdb = op->o_bd;
124 opinfo.boi_txn = ltid;
125 opinfo.boi_locker = locker;
127 opinfo.boi_acl_cache = op->o_do_not_cache;
128 op->o_private = &opinfo;
131 * Get the parent dn and see if the corresponding entry exists.
133 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
136 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
139 /* get entry or parent */
140 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
142 switch( rs->sr_err ) {
144 rs->sr_err = LDAP_ALREADY_EXISTS;
148 case DB_LOCK_DEADLOCK:
149 case DB_LOCK_NOTGRANTED:
152 rs->sr_text = "ldap server busy";
155 rs->sr_err = LDAP_OTHER;
156 rs->sr_text = "internal error";
162 if ( !bvmatch( &pdn, &p->e_nname ) ) {
163 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
165 rs->sr_ref = is_entry_referral( p )
166 ? get_entry_referrals( op, p )
168 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
170 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
173 rs->sr_err = LDAP_REFERRAL;
174 send_ldap_result( op, rs );
176 ber_bvarray_free( rs->sr_ref );
177 op->o_tmpfree( (char *)rs->sr_matched, op->o_tmpmemctx );
179 rs->sr_matched = NULL;
184 rs->sr_err = access_allowed( op, p,
185 children, NULL, ACL_WRITE, NULL );
187 if ( ! rs->sr_err ) {
188 switch( opinfo.boi_err ) {
189 case DB_LOCK_DEADLOCK:
190 case DB_LOCK_NOTGRANTED:
194 Debug( LDAP_DEBUG_TRACE,
195 "bdb_add: no write access to parent\n", 0, 0, 0 );
196 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
197 rs->sr_text = "no write access to parent";
198 goto return_results;;
201 #ifdef BDB_SUBENTRIES
202 if ( is_entry_subentry( p ) ) {
203 /* parent is a subentry, don't allow add */
204 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
206 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
207 rs->sr_text = "parent is a subentry";
208 goto return_results;;
211 if ( is_entry_alias( p ) ) {
212 /* parent is an alias, don't allow add */
213 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
215 rs->sr_err = LDAP_ALIAS_PROBLEM;
216 rs->sr_text = "parent is an alias";
217 goto return_results;;
220 if ( is_entry_referral( p ) ) {
221 /* parent is a referral, don't allow add */
222 rs->sr_matched = p->e_name.bv_val;
223 rs->sr_ref = get_entry_referrals( op, p );
225 Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
228 rs->sr_err = LDAP_REFERRAL;
229 send_ldap_result( op, rs );
231 ber_bvarray_free( rs->sr_ref );
232 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
234 rs->sr_matched = NULL;
239 #ifdef BDB_SUBENTRIES
242 /* parent must be an administrative point of the required kind */
246 /* free parent and reader lock */
247 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
253 * if not attempting to add entry at suffix or with parent ""
255 if ((( !be_isroot( op ) && !be_shadow_update(op) )
256 || pdn.bv_len > 0 ) && !is_entry_glue( op->oq_add.rs_e ))
258 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
259 pdn.bv_len == 0 ? "suffix" : "entry at root",
261 rs->sr_err = LDAP_NO_SUCH_OBJECT;
266 if ( get_assert( op ) &&
267 ( test_filter( op, op->oq_add.rs_e, get_assertion( op ))
268 != LDAP_COMPARE_TRUE ))
270 rs->sr_err = LDAP_ASSERTION_FAILED;
274 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
275 entry, NULL, ACL_WRITE, NULL );
277 if ( ! rs->sr_err ) {
278 switch( opinfo.boi_err ) {
279 case DB_LOCK_DEADLOCK:
280 case DB_LOCK_NOTGRANTED:
284 Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to entry\n",
286 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
287 rs->sr_text = "no write access to entry";
288 goto return_results;;
291 /* nested transaction */
292 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
293 bdb->bi_db_opflags );
295 if( rs->sr_err != 0 ) {
296 Debug( LDAP_DEBUG_TRACE,
297 "bdb_add: txn_begin(2) failed: %s (%d)\n",
298 db_strerror(rs->sr_err), rs->sr_err, 0 );
299 rs->sr_err = LDAP_OTHER;
300 rs->sr_text = "internal error";
305 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
306 if ( rs->sr_err != 0 ) {
307 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
308 db_strerror(rs->sr_err), rs->sr_err, 0 );
310 switch( rs->sr_err ) {
311 case DB_LOCK_DEADLOCK:
312 case DB_LOCK_NOTGRANTED:
315 rs->sr_err = LDAP_ALREADY_EXISTS;
318 rs->sr_err = LDAP_OTHER;
324 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
325 if ( rs->sr_err != 0 ) {
326 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
328 switch( rs->sr_err ) {
329 case DB_LOCK_DEADLOCK:
330 case DB_LOCK_NOTGRANTED:
333 rs->sr_err = LDAP_OTHER;
335 rs->sr_text = "entry store failed";
339 /* attribute indexes */
340 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
341 if ( rs->sr_err != LDAP_SUCCESS ) {
342 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
344 switch( rs->sr_err ) {
345 case DB_LOCK_DEADLOCK:
346 case DB_LOCK_NOTGRANTED:
349 rs->sr_err = LDAP_OTHER;
351 rs->sr_text = "index generation failed";
354 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
355 rs->sr_err = LDAP_OTHER;
356 rs->sr_text = "txn_commit(2) failed";
360 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
361 rc = bdb_csn_commit( op, rs, ltid, ei, &suffix_ei,
362 &ctxcsn_e, &ctxcsn_added, locker );
372 if( op->o_postread ) {
373 if( postread_ctrl == NULL ) {
374 postread_ctrl = &ctrls[num_ctrls++];
375 ctrls[num_ctrls] = NULL;
377 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
378 &slap_post_read_bv, postread_ctrl ) )
380 Debug( LDAP_DEBUG_TRACE,
381 "<=- bdb_add: post-read failed!\n", 0, 0, 0 );
387 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
388 rs->sr_text = "txn_abort (no-op) failed";
390 rs->sr_err = LDAP_NO_OPERATION;
396 Entry *e = entry_dup( op->ora_e );
399 nrdn.bv_val = e->e_nname.bv_val;
400 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
405 bdb_cache_add( bdb, ei, e, &nrdn, locker );
407 if ( suffix_ei == NULL ) {
411 if ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
412 if ( ctxcsn_added ) {
413 bdb_cache_add( bdb, suffix_ei, ctxcsn_e,
414 (struct berval *)&slap_ldapsync_cn_bv, locker );
418 if ( rs->sr_err == LDAP_SUCCESS && !op->o_no_psearch ) {
419 ldap_pvt_thread_rdwr_wlock( &bdb->bi_pslist_rwlock );
421 LDAP_LIST_FOREACH ( ps_list, &bdb->bi_psearch_list, o_ps_link ) {
422 rc = bdb_psearch( op, rs, ps_list, e, LDAP_PSEARCH_BY_ADD );
424 Debug( LDAP_DEBUG_TRACE,
425 "bdb_add: persistent search failed (%d,%d)\n",
429 ldap_pvt_thread_rdwr_wunlock( &bdb->bi_pslist_rwlock );
432 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
433 rs->sr_text = "txn_commit failed";
435 rs->sr_err = LDAP_SUCCESS;
440 op->o_private = NULL;
442 if ( rs->sr_err != LDAP_SUCCESS ) {
443 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s : %s (%d)\n",
444 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
445 rs->sr_err = LDAP_OTHER;
449 Debug(LDAP_DEBUG_TRACE, "bdb_add: added%s id=%08lx dn=\"%s\"\n",
450 op->o_noop ? " (no-op)" : "",
451 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
454 if( num_ctrls ) rs->sr_ctrls = ctrls;
457 send_ldap_result( op, rs );
459 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
460 ldap_pvt_thread_yield();
461 TXN_CHECKPOINT( bdb->bi_dbenv,
462 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
468 op->o_private = NULL;
471 if( postread_ctrl != NULL ) {
472 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, &op->o_tmpmemctx );
473 slap_sl_free( *postread_ctrl, &op->o_tmpmemctx );