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 BDB_LOCKER locker = 0, rlocker = 0;
44 LDAPControl **postread_ctrl = NULL;
45 LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
52 Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
53 op->oq_add.rs_e->e_name.bv_val, 0, 0);
57 /* acquire connection lock */
58 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
60 rs->sr_text = "invalid transaction identifier";
61 rs->sr_err = LDAP_X_TXN_ID_INVALID;
63 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
68 if( op->o_conn->c_txn_backend == NULL ) {
69 op->o_conn->c_txn_backend = op->o_bd;
71 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
72 rs->sr_text = "transaction cannot span multiple database contexts";
73 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
77 /* insert operation into transaction */
79 rs->sr_text = "transaction specified";
80 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
83 /* release connection lock */
84 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
87 send_ldap_result( op, rs );
96 /* check entry's schema */
97 rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
98 get_relax(op), 1, &rs->sr_text, textbuf, textlen );
99 if ( rs->sr_err != LDAP_SUCCESS ) {
100 Debug( LDAP_DEBUG_TRACE,
101 LDAP_XSTRING(bdb_add) ": entry failed schema check: "
102 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
106 /* add opattrs to shadow as well, only missing attrs will actually
107 * be added; helps compatibility with older OL versions */
108 rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
109 if ( rs->sr_err != LDAP_SUCCESS ) {
110 Debug( LDAP_DEBUG_TRACE,
111 LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
112 "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
116 subentry = is_entry_subentry( op->oq_add.rs_e );
119 * acquire an ID outside of the operation transaction
120 * to avoid serializing adds.
122 rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
123 if( rs->sr_err != 0 ) {
124 Debug( LDAP_DEBUG_TRACE,
125 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
127 rs->sr_err = LDAP_OTHER;
128 rs->sr_text = "internal error";
132 /* Get our thread locker ID */
133 rs->sr_err = LOCK_ID( bdb->bi_dbenv, &rlocker );
136 retry: /* transaction retry */
138 /* free parent and reader lock */
139 if ( p != (Entry *)&slap_entry_root ) {
140 bdb_unlocked_cache_return_entry_r( bdb, p );
144 rs->sr_err = TXN_ABORT( ltid );
146 op->o_private = NULL;
147 op->o_do_not_cache = opinfo.boi_acl_cache;
148 if( rs->sr_err != 0 ) {
149 rs->sr_err = LDAP_OTHER;
150 rs->sr_text = "internal error";
153 if ( op->o_abandon ) {
154 rs->sr_err = SLAPD_ABANDON;
157 bdb_trans_backoff( ++num_retries );
160 /* begin transaction */
161 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, <id,
162 bdb->bi_db_opflags );
164 if( rs->sr_err != 0 ) {
165 Debug( LDAP_DEBUG_TRACE,
166 LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
167 db_strerror(rs->sr_err), rs->sr_err, 0 );
168 rs->sr_err = LDAP_OTHER;
169 rs->sr_text = "internal error";
173 locker = TXN_ID ( ltid );
175 opinfo.boi_bdb = op->o_bd;
176 opinfo.boi_txn = ltid;
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;
291 /* parent must be an administrative point of the required kind */
294 /* free parent and reader lock */
295 if ( p != (Entry *)&slap_entry_root ) {
296 bdb_unlocked_cache_return_entry_r( bdb, p );
300 rs->sr_err = access_allowed( op, op->oq_add.rs_e,
301 entry, NULL, ACL_WADD, NULL );
303 if ( ! rs->sr_err ) {
304 switch( opinfo.boi_err ) {
305 case DB_LOCK_DEADLOCK:
306 case DB_LOCK_NOTGRANTED:
310 Debug( LDAP_DEBUG_TRACE,
311 LDAP_XSTRING(bdb_add) ": no write access to entry\n",
313 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
314 rs->sr_text = "no write access to entry";
315 goto return_results;;
318 /* nested transaction */
319 rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, <2,
320 bdb->bi_db_opflags );
322 if( rs->sr_err != 0 ) {
323 Debug( LDAP_DEBUG_TRACE,
324 LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
325 "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
326 rs->sr_err = LDAP_OTHER;
327 rs->sr_text = "internal error";
332 rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
333 if ( rs->sr_err != 0 ) {
334 Debug( LDAP_DEBUG_TRACE,
335 LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
336 db_strerror(rs->sr_err), rs->sr_err, 0 );
338 switch( rs->sr_err ) {
339 case DB_LOCK_DEADLOCK:
340 case DB_LOCK_NOTGRANTED:
343 rs->sr_err = LDAP_ALREADY_EXISTS;
346 rs->sr_err = LDAP_OTHER;
351 /* attribute indexes */
352 rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
353 if ( rs->sr_err != LDAP_SUCCESS ) {
354 Debug( LDAP_DEBUG_TRACE,
355 LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
357 switch( rs->sr_err ) {
358 case DB_LOCK_DEADLOCK:
359 case DB_LOCK_NOTGRANTED:
362 rs->sr_err = LDAP_OTHER;
364 rs->sr_text = "index generation failed";
369 rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
370 if ( rs->sr_err != 0 ) {
371 Debug( LDAP_DEBUG_TRACE,
372 LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
374 switch( rs->sr_err ) {
375 case DB_LOCK_DEADLOCK:
376 case DB_LOCK_NOTGRANTED:
379 rs->sr_err = LDAP_OTHER;
381 rs->sr_text = "entry store failed";
385 if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
386 rs->sr_err = LDAP_OTHER;
387 rs->sr_text = "txn_commit(2) failed";
392 if( op->o_postread ) {
393 if( postread_ctrl == NULL ) {
394 postread_ctrl = &ctrls[num_ctrls++];
395 ctrls[num_ctrls] = NULL;
397 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
398 &slap_post_read_bv, postread_ctrl ) )
400 Debug( LDAP_DEBUG_TRACE,
401 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
402 "failed!\n", 0, 0, 0 );
403 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
404 /* FIXME: is it correct to abort
405 * operation if control fails? */
412 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
413 rs->sr_text = "txn_abort (no-op) failed";
415 rs->sr_err = LDAP_X_NO_OPERATION;
423 /* pick the RDN if not suffix; otherwise pick the entire DN */
425 nrdn.bv_val = op->ora_e->e_nname.bv_val;
426 nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
428 nrdn = op->ora_e->e_nname;
431 /* Use the thread locker here, outside the txn */
432 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rlocker, &lock );
434 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
435 rs->sr_text = "txn_commit failed";
437 rs->sr_err = LDAP_SUCCESS;
442 op->o_private = NULL;
444 if ( rs->sr_err != LDAP_SUCCESS ) {
445 Debug( LDAP_DEBUG_TRACE,
446 LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
447 rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
448 rs->sr_err = LDAP_OTHER;
452 Debug(LDAP_DEBUG_TRACE,
453 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
454 op->o_noop ? " (no-op)" : "",
455 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
458 if( num_ctrls ) rs->sr_ctrls = ctrls;
461 success = rs->sr_err;
462 send_ldap_result( op, rs );
463 slap_graduate_commit_csn( op );
468 op->o_private = NULL;
470 if( success == LDAP_SUCCESS ) {
471 /* We own the entry now, and it can be purged at will
472 * Check to make sure it's the same entry we entered with.
473 * Possibly a callback may have mucked with it, although
474 * in general callbacks should treat the entry as read-only.
476 bdb_cache_return_entry_r( bdb, oe, &lock );
477 if ( op->ora_e == oe )
480 if ( bdb->bi_txn_cp_kbyte ) {
481 TXN_CHECKPOINT( bdb->bi_dbenv,
482 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
486 if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
487 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
488 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );