]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
- setup framework for monitoring of back-bdb/back-hdb stuff in their
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2006 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
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>.
15  */
16
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 int
25 bdb_add(Operation *op, SlapReply *rs )
26 {
27         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
28         struct berval   pdn;
29         Entry           *p = NULL;
30         EntryInfo       *ei;
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};
37         int subentry;
38         u_int32_t       locker = 0;
39         DB_LOCK         lock;
40
41         int             num_retries = 0;
42
43         LDAPControl **postread_ctrl = NULL;
44         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
45         int num_ctrls = 0;
46
47 #ifdef LDAP_X_TXN
48         int settle = 0;
49 #endif
50
51         Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
52                 op->oq_add.rs_e->e_name.bv_val, 0, 0);
53
54 #ifdef LDAP_X_TXN
55         if( op->o_txnSpec ) {
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;
61                         goto txnReturn;
62                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
63                         settle=1;
64                         goto txnReturn;
65                 }
66
67                 if( op->o_conn->c_txn_backend == NULL ) {
68                         op->o_conn->c_txn_backend = op->o_bd;
69
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;
73                         goto txnReturn;
74                 }
75
76                 /* insert operation into transaction */
77
78                 rs->sr_text = "transaction specified";
79                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
80
81 txnReturn:
82                 /* release connection lock */
83                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
84
85                 if( !settle ) {
86                         send_ldap_result( op, rs );
87                         return rs->sr_err;
88                 }
89         }
90 #endif
91
92         ctrls[num_ctrls] = 0;
93
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 );
97
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 );
105                 goto return_results;
106         }
107
108         subentry = is_entry_subentry( op->oq_add.rs_e );
109
110         /*
111          * acquire an ID outside of the operation transaction
112          * to avoid serializing adds.
113          */
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",
118                         rs->sr_err, 0, 0 );
119                 rs->sr_err = LDAP_OTHER;
120                 rs->sr_text = "internal error";
121                 goto return_results;
122         }
123
124         if( 0 ) {
125 retry:  /* transaction retry */
126                 if( p ) {
127                         /* free parent and reader lock */
128                         if ( p != (Entry *)&slap_entry_root ) {
129                                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
130                         }
131                         p = NULL;
132                 }
133                 rs->sr_err = TXN_ABORT( ltid );
134                 ltid = NULL;
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";
140                         goto return_results;
141                 }
142                 if ( op->o_abandon ) {
143                         rs->sr_err = SLAPD_ABANDON;
144                         goto return_results;
145                 }
146                 bdb_trans_backoff( ++num_retries );
147         }
148
149         /* begin transaction */
150         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
151                 bdb->bi_db_opflags );
152         rs->sr_text = NULL;
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";
159                 goto return_results;
160         }
161
162         locker = TXN_ID ( ltid );
163
164         opinfo.boi_bdb = op->o_bd;
165         opinfo.boi_txn = ltid;
166         opinfo.boi_locker = locker;
167         opinfo.boi_err = 0;
168         opinfo.boi_acl_cache = op->o_do_not_cache;
169         op->o_private = &opinfo;
170         
171         /*
172          * Get the parent dn and see if the corresponding entry exists.
173          */
174         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
175                 pdn = slap_empty_bv;
176         } else {
177                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
178         }
179
180         /* get entry or parent */
181         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
182                 1, locker, &lock );
183         switch( rs->sr_err ) {
184         case 0:
185                 rs->sr_err = LDAP_ALREADY_EXISTS;
186                 goto return_results;
187         case DB_NOTFOUND:
188                 break;
189         case DB_LOCK_DEADLOCK:
190         case DB_LOCK_NOTGRANTED:
191                 goto retry;
192         case LDAP_BUSY:
193                 rs->sr_text = "ldap server busy";
194                 goto return_results;
195         default:
196                 rs->sr_err = LDAP_OTHER;
197                 rs->sr_text = "internal error";
198                 goto return_results;
199         }
200
201         p = ei->bei_e;
202         if ( !p )
203                 p = (Entry *)&slap_entry_root;
204
205         if ( !bvmatch( &pdn, &p->e_nname ) ) {
206                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
207                         op->o_tmpmemctx );
208                 rs->sr_ref = is_entry_referral( p )
209                         ? get_entry_referrals( op, p )
210                         : NULL;
211                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
212                 p = NULL;
213                 Debug( LDAP_DEBUG_TRACE,
214                         LDAP_XSTRING(bdb_add) ": parent "
215                         "does not exist\n", 0, 0, 0 );
216
217                 rs->sr_err = LDAP_REFERRAL;
218                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
219                 goto return_results;
220         }
221
222         rs->sr_err = access_allowed( op, p,
223                 children, NULL, ACL_WADD, NULL );
224
225         if ( ! rs->sr_err ) {
226                 switch( opinfo.boi_err ) {
227                 case DB_LOCK_DEADLOCK:
228                 case DB_LOCK_NOTGRANTED:
229                         goto retry;
230                 }
231
232                 Debug( LDAP_DEBUG_TRACE,
233                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
234                         0, 0, 0 );
235                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
236                 rs->sr_text = "no write access to parent";
237                 goto return_results;;
238         }
239
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",
245                                 0, 0, 0 );
246                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
247                         rs->sr_text = "parent is a subentry";
248                         goto return_results;;
249                 }
250
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",
255                                 0, 0, 0 );
256                         rs->sr_err = LDAP_ALIAS_PROBLEM;
257                         rs->sr_text = "parent is an alias";
258                         goto return_results;;
259                 }
260
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,
264                                 op->o_tmpmemctx );
265                         rs->sr_ref = get_entry_referrals( op, p );
266                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
267                         p = NULL;
268                         Debug( LDAP_DEBUG_TRACE,
269                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
270                                 0, 0, 0 );
271
272                         rs->sr_err = LDAP_REFERRAL;
273                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
274                         goto return_results;
275                 }
276
277                 if ( subentry ) {
278                         /* FIXME: */
279                         /* parent must be an administrative point of the required kind */
280                 }
281         }
282
283         /* free parent and reader lock */
284         if ( p != (Entry *)&slap_entry_root ) {
285                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
286         }
287         p = NULL;
288
289         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
290                 entry, NULL, ACL_WADD, NULL );
291
292         if ( ! rs->sr_err ) {
293                 switch( opinfo.boi_err ) {
294                 case DB_LOCK_DEADLOCK:
295                 case DB_LOCK_NOTGRANTED:
296                         goto retry;
297                 }
298
299                 Debug( LDAP_DEBUG_TRACE,
300                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
301                         0, 0, 0 );
302                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
303                 rs->sr_text = "no write access to entry";
304                 goto return_results;;
305         }
306
307         /* nested transaction */
308         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
309                 bdb->bi_db_opflags );
310         rs->sr_text = NULL;
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";
317                 goto return_results;
318         }
319
320         /* dn2id index */
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 );
326
327                 switch( rs->sr_err ) {
328                 case DB_LOCK_DEADLOCK:
329                 case DB_LOCK_NOTGRANTED:
330                         goto retry;
331                 case DB_KEYEXIST:
332                         rs->sr_err = LDAP_ALREADY_EXISTS;
333                         break;
334                 default:
335                         rs->sr_err = LDAP_OTHER;
336                 }
337                 goto return_results;
338         }
339
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",
345                         0, 0, 0 );
346                 switch( rs->sr_err ) {
347                 case DB_LOCK_DEADLOCK:
348                 case DB_LOCK_NOTGRANTED:
349                         goto retry;
350                 default:
351                         rs->sr_err = LDAP_OTHER;
352                 }
353                 rs->sr_text = "index generation failed";
354                 goto return_results;
355         }
356
357         /* id2entry index */
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",
362                         0, 0, 0 );
363                 switch( rs->sr_err ) {
364                 case DB_LOCK_DEADLOCK:
365                 case DB_LOCK_NOTGRANTED:
366                         goto retry;
367                 default:
368                         rs->sr_err = LDAP_OTHER;
369                 }
370                 rs->sr_text = "entry store failed";
371                 goto return_results;
372         }
373
374         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
375                 rs->sr_err = LDAP_OTHER;
376                 rs->sr_text = "txn_commit(2) failed";
377                 goto return_results;
378         }
379
380         /* post-read */
381         if( op->o_postread ) {
382                 if( postread_ctrl == NULL ) {
383                         postread_ctrl = &ctrls[num_ctrls++];
384                         ctrls[num_ctrls] = NULL;
385                 }
386                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
387                         &slap_post_read_bv, postread_ctrl ) )
388                 {
389                         Debug( LDAP_DEBUG_TRACE,
390                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
391                                 "failed!\n", 0, 0, 0 );
392                         goto return_results;
393                 }
394         }
395
396         if ( op->o_noop ) {
397                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
398                         rs->sr_text = "txn_abort (no-op) failed";
399                 } else {
400                         rs->sr_err = LDAP_X_NO_OPERATION;
401                         ltid = NULL;
402                         goto return_results;
403                 }
404
405         } else {
406                 struct berval nrdn;
407                 Entry *e = entry_dup( op->ora_e );
408
409                 /* pick the RDN if not suffix; otherwise pick the entire DN */
410                 if (pdn.bv_len) {
411                         nrdn.bv_val = e->e_nname.bv_val;
412                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
413                 } else {
414                         nrdn = e->e_nname;
415                 }
416
417                 bdb_cache_add( bdb, ei, e, &nrdn, locker );
418
419                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
420                         rs->sr_text = "txn_commit failed";
421                 } else {
422                         rs->sr_err = LDAP_SUCCESS;
423                 }
424         }
425
426         ltid = NULL;
427         op->o_private = NULL;
428
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;
434                 goto return_results;
435         }
436
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 );
441
442         rs->sr_text = NULL;
443         if( num_ctrls ) rs->sr_ctrls = ctrls;
444
445 return_results:
446         send_ldap_result( op, rs );
447         slap_graduate_commit_csn( op );
448
449         if( ltid != NULL ) {
450                 TXN_ABORT( ltid );
451         }
452         op->o_private = NULL;
453
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 );
457         }
458
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 );
462         }
463         return rs->sr_err;
464 }