1 /* add.c - ldap BerkeleyDB back-end add routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2000-2007 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;
29 Entry *p = NULL, *oe = op->ora_e;
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};
38 u_int32_t locker = 0, rlocker = 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 );
95 /* check entry's schema */
96 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
97 get_relax(op), 1, &rs->sr_text, textbuf, textlen );
98 if ( rs->sr_err != LDAP_SUCCESS ) {
99 Debug( LDAP_DEBUG_TRACE,
100 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
101 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
105 /* add opattrs to shadow as well, only missing attrs will actually
106 * be added; helps compatibility with older OL versions */
107 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
108 if ( rs->sr_err != LDAP_SUCCESS ) {
109 Debug( LDAP_DEBUG_TRACE,
110 LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
111 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
115 subentry = is_entry_subentry( op->oq_add.rs_e );
118 * acquire an ID outside of the operation transaction
119 * to avoid serializing adds.
121 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
122 if( rs->sr_err != 0 ) {
123 Debug( LDAP_DEBUG_TRACE,
124 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
126 rs->sr_err = LDAP_OTHER;
127 rs->sr_text = "internal error";
131 /* Get our thread locker ID */
132 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &rlocker );
135 retry: /* transaction retry */
137 /* free parent and reader lock */
138 if ( p != (Entry *)&slap_entry_root ) {
139 bdb_unlocked_cache_return_entry_r( bdb, p );
143 rs->sr_err = TXN_ABORT( ltid );
145 op->o_private = NULL;
146 op->o_do_not_cache = opinfo.boi_acl_cache;
147 if( rs->sr_err != 0 ) {
148 rs->sr_err = LDAP_OTHER;
149 rs->sr_text = "internal error";
152 if ( op->o_abandon ) {
153 rs->sr_err = SLAPD_ABANDON;
156 bdb_trans_backoff( ++num_retries );
159 /* begin transaction */
160 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
161 bdb->bi_db_opflags );
163 if( rs->sr_err != 0 ) {
164 Debug( LDAP_DEBUG_TRACE,
165 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
166 db_strerror(rs->sr_err), rs->sr_err, 0 );
167 rs->sr_err = LDAP_OTHER;
168 rs->sr_text = "internal error";
172 locker = TXN_ID ( ltid );
174 opinfo.boi_bdb = op->o_bd;
175 opinfo.boi_txn = ltid;
176 opinfo.boi_locker = locker;
178 opinfo.boi_acl_cache = op->o_do_not_cache;
179 op->o_private = &opinfo;
182 * Get the parent dn and see if the corresponding entry exists.
184 if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
187 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
190 /* get entry or parent */
191 rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
193 switch( rs->sr_err ) {
195 rs->sr_err = LDAP_ALREADY_EXISTS;
199 case DB_LOCK_DEADLOCK:
200 case DB_LOCK_NOTGRANTED:
203 rs->sr_text = "ldap server busy";
206 rs->sr_err = LDAP_OTHER;
207 rs->sr_text = "internal error";
213 p = (Entry *)&slap_entry_root;
215 if ( !bvmatch( &pdn, &p->e_nname ) ) {
216 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
218 rs->sr_ref = is_entry_referral( p )
219 ? get_entry_referrals( op, p )
221 bdb_unlocked_cache_return_entry_r( bdb, p );
223 Debug( LDAP_DEBUG_TRACE,
224 LDAP_XSTRING(bdb_add) ": parent "
225 "does not exist\n", 0, 0, 0 );
227 rs->sr_err = LDAP_REFERRAL;
228 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
232 rs->sr_err = access_allowed( op, p,
233 children, NULL, ACL_WADD, NULL );
235 if ( ! rs->sr_err ) {
236 switch( opinfo.boi_err ) {
237 case DB_LOCK_DEADLOCK:
238 case DB_LOCK_NOTGRANTED:
242 Debug( LDAP_DEBUG_TRACE,
243 LDAP_XSTRING(bdb_add) ": no write access to parent\n",
245 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
246 rs->sr_text = "no write access to parent";
247 goto return_results;;
250 if ( p != (Entry *)&slap_entry_root ) {
251 if ( is_entry_subentry( p ) ) {
252 /* parent is a subentry, don't allow add */
253 Debug( LDAP_DEBUG_TRACE,
254 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
256 rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
257 rs->sr_text = "parent is a subentry";
258 goto return_results;;
261 if ( is_entry_alias( p ) ) {
262 /* parent is an alias, don't allow add */
263 Debug( LDAP_DEBUG_TRACE,
264 LDAP_XSTRING(bdb_add) ": parent is alias\n",
266 rs->sr_err = LDAP_ALIAS_PROBLEM;
267 rs->sr_text = "parent is an alias";
268 goto return_results;;
271 if ( is_entry_referral( p ) ) {
272 /* parent is a referral, don't allow add */
273 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
275 rs->sr_ref = get_entry_referrals( op, p );
276 bdb_unlocked_cache_return_entry_r( bdb, p );
278 Debug( LDAP_DEBUG_TRACE,
279 LDAP_XSTRING(bdb_add) ": parent is referral\n",
282 rs->sr_err = LDAP_REFERRAL;
283 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
289 /* parent must be an administrative point of the required kind */
293 /* free parent and reader lock */
294 if ( p != (Entry *)&slap_entry_root ) {
295 bdb_unlocked_cache_return_entry_r( bdb, p );
299 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
300 entry, NULL, ACL_WADD, NULL );
302 if ( ! rs->sr_err ) {
303 switch( opinfo.boi_err ) {
304 case DB_LOCK_DEADLOCK:
305 case DB_LOCK_NOTGRANTED:
309 Debug( LDAP_DEBUG_TRACE,
310 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
312 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
313 rs->sr_text = "no write access to entry";
314 goto return_results;;
317 /* nested transaction */
318 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
319 bdb->bi_db_opflags );
321 if( rs->sr_err != 0 ) {
322 Debug( LDAP_DEBUG_TRACE,
323 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
324 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
325 rs->sr_err = LDAP_OTHER;
326 rs->sr_text = "internal error";
331 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
332 if ( rs->sr_err != 0 ) {
333 Debug( LDAP_DEBUG_TRACE,
334 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
335 db_strerror(rs->sr_err), rs->sr_err, 0 );
337 switch( rs->sr_err ) {
338 case DB_LOCK_DEADLOCK:
339 case DB_LOCK_NOTGRANTED:
342 rs->sr_err = LDAP_ALREADY_EXISTS;
345 rs->sr_err = LDAP_OTHER;
350 /* attribute indexes */
351 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
352 if ( rs->sr_err != LDAP_SUCCESS ) {
353 Debug( LDAP_DEBUG_TRACE,
354 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
356 switch( rs->sr_err ) {
357 case DB_LOCK_DEADLOCK:
358 case DB_LOCK_NOTGRANTED:
361 rs->sr_err = LDAP_OTHER;
363 rs->sr_text = "index generation failed";
368 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
369 if ( rs->sr_err != 0 ) {
370 Debug( LDAP_DEBUG_TRACE,
371 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
373 switch( rs->sr_err ) {
374 case DB_LOCK_DEADLOCK:
375 case DB_LOCK_NOTGRANTED:
378 rs->sr_err = LDAP_OTHER;
380 rs->sr_text = "entry store failed";
384 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
385 rs->sr_err = LDAP_OTHER;
386 rs->sr_text = "txn_commit(2) failed";
391 if( op->o_postread ) {
392 if( postread_ctrl == NULL ) {
393 postread_ctrl = &ctrls[num_ctrls++];
394 ctrls[num_ctrls] = NULL;
396 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
397 &slap_post_read_bv, postread_ctrl ) )
399 Debug( LDAP_DEBUG_TRACE,
400 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
401 "failed!\n", 0, 0, 0 );
402 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
403 /* FIXME: is it correct to abort
404 * operation if control fails? */
411 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
412 rs->sr_text = "txn_abort (no-op) failed";
414 rs->sr_err = LDAP_X_NO_OPERATION;
422 /* pick the RDN if not suffix; otherwise pick the entire DN */
424 nrdn.bv_val = op->ora_e->e_nname.bv_val;
425 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
427 nrdn = op->ora_e->e_nname;
430 /* Use the thread locker here, outside the txn */
431 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rlocker, &lock );
433 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
434 rs->sr_text = "txn_commit failed";
436 rs->sr_err = LDAP_SUCCESS;
441 op->o_private = NULL;
443 if ( rs->sr_err != LDAP_SUCCESS ) {
444 Debug( LDAP_DEBUG_TRACE,
445 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
446 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
447 rs->sr_err = LDAP_OTHER;
451 Debug(LDAP_DEBUG_TRACE,
452 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
453 op->o_noop ? " (no-op)" : "",
454 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
457 if( num_ctrls ) rs->sr_ctrls = ctrls;
460 send_ldap_result( op, rs );
461 slap_graduate_commit_csn( op );
466 op->o_private = NULL;
468 if( rs->sr_err == LDAP_SUCCESS ) {
469 /* We own the entry now, and it can be purged at will
470 * Check to make sure it's the same entry we entered with.
471 * Possibly a callback may have mucked with it, although
472 * in general callbacks should treat the entry as read-only.
474 bdb_cache_return_entry_r( bdb, oe, &lock );
475 if ( op->ora_e == oe )
478 if ( bdb->bi_txn_cp_kbyte ) {
479 TXN_CHECKPOINT( bdb->bi_dbenv,
480 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
484 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
485 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
486 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );