1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2006 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>
25 bdb_add(Operation *op, SlapReply *rs )
27 struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
31 char textbuf[SLAP_TEXT_BUFLEN];
32 size_t textlen = sizeof textbuf;
33 AttributeDescription *children = slap_schema.si_ad_children;
34 AttributeDescription *entry = slap_schema.si_ad_entry;
35 DB_TXN *ltid = NULL, *lt2;
36 struct bdb_op_info opinfo = {0};
43 LDAPControl **postread_ctrl = NULL;
44 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
51 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
52 op->oq_add.rs_e->e_name.bv_val, 0, 0);
56 /* acquire connection lock */
57 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
58 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
59 rs->sr_text = "invalid transaction identifier";
60 rs->sr_err = LDAP_X_TXN_ID_INVALID;
62 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
67 if( op->o_conn->c_txn_backend == NULL ) {
68 op->o_conn->c_txn_backend = op->o_bd;
70 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
71 rs->sr_text = "transaction cannot span multiple database contexts";
72 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
76 /* insert operation into transaction */
78 rs->sr_text = "transaction specified";
79 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
82 /* release connection lock */
83 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
86 send_ldap_result( op, rs );
94 /* add opattrs to shadow as well, only missing attrs will actually
95 * be added; helps compatibility with older OL versions */
96 slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
98 /* check entry's schema */
99 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
100 get_relax(op), &rs->sr_text, textbuf, textlen );
101 if ( rs->sr_err != LDAP_SUCCESS ) {
102 Debug( LDAP_DEBUG_TRACE,
103 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
104 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
108 subentry = is_entry_subentry( op->oq_add.rs_e );
111 * acquire an ID outside of the operation transaction
112 * to avoid serializing adds.
114 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
115 if( rs->sr_err != 0 ) {
116 Debug( LDAP_DEBUG_TRACE,
117 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
119 rs->sr_err = LDAP_OTHER;
120 rs->sr_text = "internal error";
125 retry: /* transaction retry */
127 /* free parent and reader lock */
128 if ( p != (Entry *)&slap_entry_root ) {
129 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
133 rs->sr_err = TXN_ABORT( ltid );
135 op->o_private = NULL;
136 op->o_do_not_cache = opinfo.boi_acl_cache;
137 if( rs->sr_err != 0 ) {
138 rs->sr_err = LDAP_OTHER;
139 rs->sr_text = "internal error";
142 if ( op->o_abandon ) {
143 rs->sr_err = SLAPD_ABANDON;
146 bdb_trans_backoff( ++num_retries );
149 /* begin transaction */
150 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
151 bdb->bi_db_opflags );
153 if( rs->sr_err != 0 ) {
154 Debug( LDAP_DEBUG_TRACE,
155 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
156 db_strerror(rs->sr_err), rs->sr_err, 0 );
157 rs->sr_err = LDAP_OTHER;
158 rs->sr_text = "internal error";
162 locker = TXN_ID ( ltid );
164 opinfo.boi_bdb = op->o_bd;
165 opinfo.boi_txn = ltid;
166 opinfo.boi_locker = locker;
168 opinfo.boi_acl_cache = op->o_do_not_cache;
169 op->o_private = &opinfo;
172 * Get the parent dn and see if the corresponding entry exists.
174 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
177 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
180 /* get entry or parent */
181 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
183 switch( rs->sr_err ) {
185 rs->sr_err = LDAP_ALREADY_EXISTS;
189 case DB_LOCK_DEADLOCK:
190 case DB_LOCK_NOTGRANTED:
193 rs->sr_text = "ldap server busy";
196 rs->sr_err = LDAP_OTHER;
197 rs->sr_text = "internal error";
203 p = (Entry *)&slap_entry_root;
205 if ( !bvmatch( &pdn, &p->e_nname ) ) {
206 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
208 rs->sr_ref = is_entry_referral( p )
209 ? get_entry_referrals( op, p )
211 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
213 Debug( LDAP_DEBUG_TRACE,
214 LDAP_XSTRING(bdb_add) ": parent "
215 "does not exist\n", 0, 0, 0 );
217 rs->sr_err = LDAP_REFERRAL;
218 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
222 rs->sr_err = access_allowed( op, p,
223 children, NULL, ACL_WADD, NULL );
225 if ( ! rs->sr_err ) {
226 switch( opinfo.boi_err ) {
227 case DB_LOCK_DEADLOCK:
228 case DB_LOCK_NOTGRANTED:
232 Debug( LDAP_DEBUG_TRACE,
233 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
235 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
236 rs->sr_text = "no write access to parent";
237 goto return_results;;
240 if ( p != (Entry *)&slap_entry_root ) {
241 if ( is_entry_subentry( p ) ) {
242 /* parent is a subentry, don't allow add */
243 Debug( LDAP_DEBUG_TRACE,
244 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
246 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
247 rs->sr_text = "parent is a subentry";
248 goto return_results;;
251 if ( is_entry_alias( p ) ) {
252 /* parent is an alias, don't allow add */
253 Debug( LDAP_DEBUG_TRACE,
254 LDAP_XSTRING(bdb_add) ": parent is alias\n",
256 rs->sr_err = LDAP_ALIAS_PROBLEM;
257 rs->sr_text = "parent is an alias";
258 goto return_results;;
261 if ( is_entry_referral( p ) ) {
262 /* parent is a referral, don't allow add */
263 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
265 rs->sr_ref = get_entry_referrals( op, p );
266 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
268 Debug( LDAP_DEBUG_TRACE,
269 LDAP_XSTRING(bdb_add) ": parent is referral\n",
272 rs->sr_err = LDAP_REFERRAL;
273 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
279 /* parent must be an administrative point of the required kind */
283 /* free parent and reader lock */
284 if ( p != (Entry *)&slap_entry_root ) {
285 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
289 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
290 entry, NULL, ACL_WADD, NULL );
292 if ( ! rs->sr_err ) {
293 switch( opinfo.boi_err ) {
294 case DB_LOCK_DEADLOCK:
295 case DB_LOCK_NOTGRANTED:
299 Debug( LDAP_DEBUG_TRACE,
300 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
302 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
303 rs->sr_text = "no write access to entry";
304 goto return_results;;
307 /* nested transaction */
308 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
309 bdb->bi_db_opflags );
311 if( rs->sr_err != 0 ) {
312 Debug( LDAP_DEBUG_TRACE,
313 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
314 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
315 rs->sr_err = LDAP_OTHER;
316 rs->sr_text = "internal error";
321 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
322 if ( rs->sr_err != 0 ) {
323 Debug( LDAP_DEBUG_TRACE,
324 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
325 db_strerror(rs->sr_err), rs->sr_err, 0 );
327 switch( rs->sr_err ) {
328 case DB_LOCK_DEADLOCK:
329 case DB_LOCK_NOTGRANTED:
332 rs->sr_err = LDAP_ALREADY_EXISTS;
335 rs->sr_err = LDAP_OTHER;
340 /* attribute indexes */
341 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
342 if ( rs->sr_err != LDAP_SUCCESS ) {
343 Debug( LDAP_DEBUG_TRACE,
344 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
346 switch( rs->sr_err ) {
347 case DB_LOCK_DEADLOCK:
348 case DB_LOCK_NOTGRANTED:
351 rs->sr_err = LDAP_OTHER;
353 rs->sr_text = "index generation failed";
358 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
359 if ( rs->sr_err != 0 ) {
360 Debug( LDAP_DEBUG_TRACE,
361 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
363 switch( rs->sr_err ) {
364 case DB_LOCK_DEADLOCK:
365 case DB_LOCK_NOTGRANTED:
368 rs->sr_err = LDAP_OTHER;
370 rs->sr_text = "entry store failed";
374 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
375 rs->sr_err = LDAP_OTHER;
376 rs->sr_text = "txn_commit(2) failed";
381 if( op->o_postread ) {
382 if( postread_ctrl == NULL ) {
383 postread_ctrl = &ctrls[num_ctrls++];
384 ctrls[num_ctrls] = NULL;
386 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
387 &slap_post_read_bv, postread_ctrl ) )
389 Debug( LDAP_DEBUG_TRACE,
390 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
391 "failed!\n", 0, 0, 0 );
397 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
398 rs->sr_text = "txn_abort (no-op) failed";
400 rs->sr_err = LDAP_X_NO_OPERATION;
407 Entry *e = entry_dup( op->ora_e );
409 /* pick the RDN if not suffix; otherwise pick the entire DN */
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(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
420 rs->sr_text = "txn_commit failed";
422 rs->sr_err = LDAP_SUCCESS;
427 op->o_private = NULL;
429 if ( rs->sr_err != LDAP_SUCCESS ) {
430 Debug( LDAP_DEBUG_TRACE,
431 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
432 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
433 rs->sr_err = LDAP_OTHER;
437 Debug(LDAP_DEBUG_TRACE,
438 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
439 op->o_noop ? " (no-op)" : "",
440 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
443 if( num_ctrls ) rs->sr_ctrls = ctrls;
446 send_ldap_result( op, rs );
447 slap_graduate_commit_csn( op );
452 op->o_private = NULL;
454 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
455 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
456 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
459 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
460 TXN_CHECKPOINT( bdb->bi_dbenv,
461 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );