]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Do deadlock detection on any conflict, instead of in a separate thread
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_add(
18         BackendDB       *be,
19         Connection      *conn,
20         Operation       *op,
21         Entry   *e )
22 {
23         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
24         struct berval   pdn;
25         Entry           *p = NULL;
26         int                     rc; 
27         const char      *text;
28         char textbuf[SLAP_TEXT_BUFLEN];
29         size_t textlen = sizeof textbuf;
30         AttributeDescription *children = slap_schema.si_ad_children;
31         DB_TXN          *ltid = NULL;
32         struct bdb_op_info opinfo;
33         int subentry;
34
35         Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
36
37         /* check entry's schema */
38         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
39         if ( rc != LDAP_SUCCESS ) {
40                 Debug( LDAP_DEBUG_TRACE,
41                         "bdb_add: entry failed schema check: %s (%d)\n",
42                         text, rc, 0 );
43                 goto return_results;
44         }
45
46         subentry = is_entry_subentry( e );
47
48         /*
49          * acquire an ID outside of the operation transaction
50          * to avoid serializing adds.
51          */
52         rc = bdb_next_id( be, NULL, &e->e_id );
53         if( rc != 0 ) {
54                 Debug( LDAP_DEBUG_TRACE,
55                         "bdb_add: next_id failed (%d)\n",
56                         rc, 0, 0 );
57                 rc = LDAP_OTHER;
58                 text = "internal error";
59                 goto return_results;
60         }
61
62         if( 0 ) {
63                 /* transaction retry */
64 retry:  rc = txn_abort( ltid );
65                 ltid = NULL;
66                 op->o_private = NULL;
67                 if( rc != 0 ) {
68                         rc = LDAP_OTHER;
69                         text = "internal error";
70                         goto return_results;
71                 }
72         }
73
74         /* begin transaction */
75         if( bdb->bi_txn ) {
76                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
77                         bdb->bi_db_opflags );
78                 text = NULL;
79                 if( rc != 0 ) {
80                         Debug( LDAP_DEBUG_TRACE,
81                                 "bdb_add: txn_begin failed: %s (%d)\n",
82                                 db_strerror(rc), rc, 0 );
83                         rc = LDAP_OTHER;
84                         text = "internal error";
85                         goto return_results;
86                 }
87         }
88
89         opinfo.boi_bdb = be;
90         opinfo.boi_txn = ltid;
91         opinfo.boi_err = 0;
92         op->o_private = &opinfo;
93         
94         /*
95          * Get the parent dn and see if the corresponding entry exists.
96          * If the parent does not exist, only allow the "root" user to
97          * add the entry.
98          */
99         if ( be_issuffix( be, e->e_nname.bv_val ) ) {
100                 pdn.bv_len = 0;
101                 pdn.bv_val = "";
102         } else {
103                 rc = dnParent( e->e_nname.bv_val, &pdn.bv_val );
104                 if ( rc != LDAP_SUCCESS ) {
105                         text = "internal error";
106                         goto return_results;
107                 }
108                 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_nname.bv_val);
109         }
110
111         if( pdn.bv_len != 0 ) {
112                 Entry *matched = NULL;
113
114                 /* get parent */
115                 rc = bdb_dn2entry( be, ltid, &pdn, &p, &matched, 0 );
116
117                 switch( rc ) {
118                 case 0:
119                 case DB_NOTFOUND:
120                         break;
121                 case DB_LOCK_DEADLOCK:
122                 case DB_LOCK_NOTGRANTED:
123                         goto retry;
124                 default:
125                         rc = LDAP_OTHER;
126                         text = "internal error";
127                         goto return_results;
128                 }
129
130                 if ( p == NULL ) {
131                         char *matched_dn = NULL;
132                         BerVarray refs;
133
134                         if ( matched != NULL ) {
135                                 matched_dn = ch_strdup( matched->e_dn );
136                                 refs = is_entry_referral( matched )
137                                         ? get_entry_referrals( be, conn, op, matched )
138                                         : NULL;
139                                 bdb_entry_return( be, matched );
140                                 matched = NULL;
141
142                         } else {
143                                 refs = referral_rewrite( default_referral,
144                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
145                         }
146
147                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
148                                 0, 0, 0 );
149
150                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
151                                 matched_dn, NULL, refs, NULL );
152
153                         ber_bvarray_free( refs );
154                         ch_free( matched_dn );
155
156                         goto done;
157                 }
158
159                 if ( ! access_allowed( be, conn, op, p,
160                         children, NULL, ACL_WRITE ) )
161                 {
162                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
163                                 0, 0, 0 );
164                         rc = LDAP_INSUFFICIENT_ACCESS;
165                         text = "no write access to parent";
166                         goto return_results;;
167                 }
168
169                 if ( is_entry_subentry( p ) ) {
170                         /* parent is a subentry, don't allow add */
171                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
172                                 0, 0, 0 );
173                         rc = LDAP_OBJECT_CLASS_VIOLATION;
174                         text = "parent is a subentry";
175                         goto return_results;;
176                 }
177
178                 if ( is_entry_alias( p ) ) {
179                         /* parent is an alias, don't allow add */
180                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
181                                 0, 0, 0 );
182                         rc = LDAP_ALIAS_PROBLEM;
183                         text = "parent is an alias";
184                         goto return_results;;
185                 }
186
187                 if ( is_entry_referral( p ) ) {
188                         /* parent is a referral, don't allow add */
189                         char *matched_dn = ch_strdup( p->e_dn );
190                         BerVarray refs = is_entry_referral( p )
191                                 ? get_entry_referrals( be, conn, op, p )
192                                 : NULL;
193
194                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
195                                 0, 0, 0 );
196
197                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
198                                 matched_dn, NULL, refs, NULL );
199
200                         ber_bvarray_free( refs );
201                         free( matched_dn );
202                         goto done;
203                 }
204
205                 if ( subentry ) {
206                         /* FIXME: */
207                         /* parent must be an administrative point of the required kind */
208                 }
209
210                 /* free parent and writer lock */
211                 bdb_entry_return( be, p );
212                 p = NULL;
213
214         } else {
215                 /*
216                  * no parent!
217                  *      must be adding entry at suffix or with parent ""
218                  */
219                 if ( !be_isroot( be, &op->o_ndn )) {
220                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
221                                 p = (Entry *)&slap_entry_root;
222
223                                 /* check parent for "children" acl */
224                                 rc = access_allowed( be, conn, op, p,
225                                         children, NULL, ACL_WRITE );
226                                 p = NULL;
227
228                                 if ( ! rc ) {
229                                         Debug( LDAP_DEBUG_TRACE,
230                                                 "bdb_add: no write access to parent\n",
231                                                 0, 0, 0 );
232                                         rc = LDAP_INSUFFICIENT_ACCESS;
233                                         text = "no write access to parent";
234                                         goto return_results;;
235                                 }
236
237                         } else {
238                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
239                                         pdn.bv_len == 0 ? "suffix" : "entry at root",
240                                         0, 0 );
241                                 rc = LDAP_INSUFFICIENT_ACCESS;
242                                 goto return_results;
243                         }
244                 }
245
246                 if( subentry ) {
247                         Debug( LDAP_DEBUG_TRACE,
248                                 "bdb_add: no parent, cannot add subentry\n",
249                                 0, 0, 0 );
250                         rc = LDAP_INSUFFICIENT_ACCESS;
251                         text = "no parent, cannot add subentry";
252                         goto return_results;;
253                 }
254         }
255
256         /* dn2id index */
257         rc = bdb_dn2id_add( be, ltid, &pdn, e );
258         if ( rc != 0 ) {
259                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
260                         db_strerror(rc), rc, 0 );
261
262                 switch( rc ) {
263                 case DB_LOCK_DEADLOCK:
264                 case DB_LOCK_NOTGRANTED:
265                         goto retry;
266                 case DB_KEYEXIST:
267                         rc = LDAP_ALREADY_EXISTS;
268                         break;
269                 default:
270                         rc = LDAP_OTHER;
271                 }
272                 goto return_results;
273         }
274
275         /* id2entry index */
276         rc = bdb_id2entry_add( be, ltid, e );
277         if ( rc != 0 ) {
278                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
279                         0, 0, 0 );
280                 switch( rc ) {
281                 case DB_LOCK_DEADLOCK:
282                 case DB_LOCK_NOTGRANTED:
283                         goto retry;
284                 default:
285                         rc = LDAP_OTHER;
286                 }
287                 text = "entry store failed";
288                 goto return_results;
289         }
290
291         /* attribute indexes */
292         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
293         if ( rc != LDAP_SUCCESS ) {
294                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
295                         0, 0, 0 );
296                 switch( rc ) {
297                 case DB_LOCK_DEADLOCK:
298                 case DB_LOCK_NOTGRANTED:
299                         goto retry;
300                 default:
301                         rc = LDAP_OTHER;
302                 }
303                 text = "index generation failed";
304                 goto return_results;
305         }
306
307         if( bdb->bi_txn ) {
308                 rc = txn_commit( ltid, 0 );
309         }
310         ltid = NULL;
311         op->o_private = NULL;
312
313         if( rc != 0 ) {
314                 Debug( LDAP_DEBUG_TRACE,
315                         "bdb_add: txn_commit failed: %s (%d)\n",
316                         db_strerror(rc), rc, 0 );
317                 rc = LDAP_OTHER;
318                 text = "commit failed";
319
320         } else {
321                 Debug( LDAP_DEBUG_TRACE,
322                         "bdb_add: added id=%08lx dn=\"%s\"\n",
323                         e->e_id, e->e_dn, 0 );
324                 rc = LDAP_SUCCESS;
325                 text = NULL;
326         }
327
328 return_results:
329         send_ldap_result( conn, op, rc,
330                 NULL, text, NULL, NULL );
331
332         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
333                 ldap_pvt_thread_yield();
334                 TXN_CHECKPOINT( bdb->bi_dbenv,
335                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
336         }
337
338 done:
339         if (p != NULL) {
340                 /* free parent and writer lock */
341                 bdb_entry_return( be, p ); 
342         }
343
344         if( ltid != NULL ) {
345                 txn_abort( ltid );
346                 op->o_private = NULL;
347         }
348
349         return rc;
350 }