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 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
97 if ( rs->sr_err != LDAP_SUCCESS ) {
98 Debug( LDAP_DEBUG_TRACE,
99 LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
100 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
104 /* check entry's schema */
105 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
106 get_relax(op), &rs->sr_text, textbuf, textlen );
107 if ( rs->sr_err != LDAP_SUCCESS ) {
108 Debug( LDAP_DEBUG_TRACE,
109 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
110 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
114 subentry = is_entry_subentry( op->oq_add.rs_e );
117 * acquire an ID outside of the operation transaction
118 * to avoid serializing adds.
120 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
121 if( rs->sr_err != 0 ) {
122 Debug( LDAP_DEBUG_TRACE,
123 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
125 rs->sr_err = LDAP_OTHER;
126 rs->sr_text = "internal error";
131 retry: /* transaction retry */
133 /* free parent and reader lock */
134 if ( p != (Entry *)&slap_entry_root ) {
135 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
139 rs->sr_err = TXN_ABORT( ltid );
141 op->o_private = NULL;
142 op->o_do_not_cache = opinfo.boi_acl_cache;
143 if( rs->sr_err != 0 ) {
144 rs->sr_err = LDAP_OTHER;
145 rs->sr_text = "internal error";
148 if ( op->o_abandon ) {
149 rs->sr_err = SLAPD_ABANDON;
152 bdb_trans_backoff( ++num_retries );
155 /* begin transaction */
156 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
157 bdb->bi_db_opflags );
159 if( rs->sr_err != 0 ) {
160 Debug( LDAP_DEBUG_TRACE,
161 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
162 db_strerror(rs->sr_err), rs->sr_err, 0 );
163 rs->sr_err = LDAP_OTHER;
164 rs->sr_text = "internal error";
168 locker = TXN_ID ( ltid );
170 opinfo.boi_bdb = op->o_bd;
171 opinfo.boi_txn = ltid;
172 opinfo.boi_locker = locker;
174 opinfo.boi_acl_cache = op->o_do_not_cache;
175 op->o_private = &opinfo;
178 * Get the parent dn and see if the corresponding entry exists.
180 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
183 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
186 /* get entry or parent */
187 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
189 switch( rs->sr_err ) {
191 rs->sr_err = LDAP_ALREADY_EXISTS;
195 case DB_LOCK_DEADLOCK:
196 case DB_LOCK_NOTGRANTED:
199 rs->sr_text = "ldap server busy";
202 rs->sr_err = LDAP_OTHER;
203 rs->sr_text = "internal error";
209 p = (Entry *)&slap_entry_root;
211 if ( !bvmatch( &pdn, &p->e_nname ) ) {
212 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
214 rs->sr_ref = is_entry_referral( p )
215 ? get_entry_referrals( op, p )
217 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
219 Debug( LDAP_DEBUG_TRACE,
220 LDAP_XSTRING(bdb_add) ": parent "
221 "does not exist\n", 0, 0, 0 );
223 rs->sr_err = LDAP_REFERRAL;
224 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
228 rs->sr_err = access_allowed( op, p,
229 children, NULL, ACL_WADD, NULL );
231 if ( ! rs->sr_err ) {
232 switch( opinfo.boi_err ) {
233 case DB_LOCK_DEADLOCK:
234 case DB_LOCK_NOTGRANTED:
238 Debug( LDAP_DEBUG_TRACE,
239 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
241 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
242 rs->sr_text = "no write access to parent";
243 goto return_results;;
246 if ( p != (Entry *)&slap_entry_root ) {
247 if ( is_entry_subentry( p ) ) {
248 /* parent is a subentry, don't allow add */
249 Debug( LDAP_DEBUG_TRACE,
250 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
252 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
253 rs->sr_text = "parent is a subentry";
254 goto return_results;;
257 if ( is_entry_alias( p ) ) {
258 /* parent is an alias, don't allow add */
259 Debug( LDAP_DEBUG_TRACE,
260 LDAP_XSTRING(bdb_add) ": parent is alias\n",
262 rs->sr_err = LDAP_ALIAS_PROBLEM;
263 rs->sr_text = "parent is an alias";
264 goto return_results;;
267 if ( is_entry_referral( p ) ) {
268 /* parent is a referral, don't allow add */
269 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
271 rs->sr_ref = get_entry_referrals( op, p );
272 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
274 Debug( LDAP_DEBUG_TRACE,
275 LDAP_XSTRING(bdb_add) ": parent is referral\n",
278 rs->sr_err = LDAP_REFERRAL;
279 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
285 /* parent must be an administrative point of the required kind */
289 /* free parent and reader lock */
290 if ( p != (Entry *)&slap_entry_root ) {
291 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
295 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
296 entry, NULL, ACL_WADD, NULL );
298 if ( ! rs->sr_err ) {
299 switch( opinfo.boi_err ) {
300 case DB_LOCK_DEADLOCK:
301 case DB_LOCK_NOTGRANTED:
305 Debug( LDAP_DEBUG_TRACE,
306 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
308 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
309 rs->sr_text = "no write access to entry";
310 goto return_results;;
313 /* nested transaction */
314 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
315 bdb->bi_db_opflags );
317 if( rs->sr_err != 0 ) {
318 Debug( LDAP_DEBUG_TRACE,
319 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
320 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
321 rs->sr_err = LDAP_OTHER;
322 rs->sr_text = "internal error";
327 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
328 if ( rs->sr_err != 0 ) {
329 Debug( LDAP_DEBUG_TRACE,
330 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
331 db_strerror(rs->sr_err), rs->sr_err, 0 );
333 switch( rs->sr_err ) {
334 case DB_LOCK_DEADLOCK:
335 case DB_LOCK_NOTGRANTED:
338 rs->sr_err = LDAP_ALREADY_EXISTS;
341 rs->sr_err = LDAP_OTHER;
346 /* attribute indexes */
347 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
348 if ( rs->sr_err != LDAP_SUCCESS ) {
349 Debug( LDAP_DEBUG_TRACE,
350 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
352 switch( rs->sr_err ) {
353 case DB_LOCK_DEADLOCK:
354 case DB_LOCK_NOTGRANTED:
357 rs->sr_err = LDAP_OTHER;
359 rs->sr_text = "index generation failed";
364 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
365 if ( rs->sr_err != 0 ) {
366 Debug( LDAP_DEBUG_TRACE,
367 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
369 switch( rs->sr_err ) {
370 case DB_LOCK_DEADLOCK:
371 case DB_LOCK_NOTGRANTED:
374 rs->sr_err = LDAP_OTHER;
376 rs->sr_text = "entry store failed";
380 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
381 rs->sr_err = LDAP_OTHER;
382 rs->sr_text = "txn_commit(2) failed";
387 if( op->o_postread ) {
388 if( postread_ctrl == NULL ) {
389 postread_ctrl = &ctrls[num_ctrls++];
390 ctrls[num_ctrls] = NULL;
392 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
393 &slap_post_read_bv, postread_ctrl ) )
395 Debug( LDAP_DEBUG_TRACE,
396 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
397 "failed!\n", 0, 0, 0 );
398 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
399 /* FIXME: is it correct to abort
400 * operation if control fails? */
407 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
408 rs->sr_text = "txn_abort (no-op) failed";
410 rs->sr_err = LDAP_X_NO_OPERATION;
417 Entry *e = entry_dup( op->ora_e );
419 /* pick the RDN if not suffix; otherwise pick the entire DN */
421 nrdn.bv_val = e->e_nname.bv_val;
422 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
427 bdb_cache_add( bdb, ei, e, &nrdn, locker );
429 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
430 rs->sr_text = "txn_commit failed";
432 rs->sr_err = LDAP_SUCCESS;
437 op->o_private = NULL;
439 if ( rs->sr_err != LDAP_SUCCESS ) {
440 Debug( LDAP_DEBUG_TRACE,
441 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
442 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
443 rs->sr_err = LDAP_OTHER;
447 Debug(LDAP_DEBUG_TRACE,
448 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
449 op->o_noop ? " (no-op)" : "",
450 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
453 if( num_ctrls ) rs->sr_ctrls = ctrls;
456 send_ldap_result( op, rs );
457 slap_graduate_commit_csn( op );
462 op->o_private = NULL;
464 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
465 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
466 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
469 if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp_kbyte ) {
470 TXN_CHECKPOINT( bdb->bi_dbenv,
471 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );