]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/add.c
Integration of the BDB2 backend into the new init/startup/shutdown schema.
[openldap] / servers / slapd / back-bdb2 / add.c
1 /* add.c - ldap bdb2 back-end add routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8 #include <ac/string.h>
9
10 #include "slap.h"
11 #include "back-bdb2.h"
12 #include "proto-back-bdb2.h"
13
14 static int
15 bdb2i_back_add_internal(
16     BackendDB   *be,
17     Connection  *conn,
18     Operation   *op,
19     Entry       *e
20 )
21 {
22         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
23         char            *pdn;
24         Entry           *p = NULL;
25         int                     rootlock = 0;
26         int                     rc = -1; 
27
28         Debug(LDAP_DEBUG_ARGS, "==> bdb2i_back_add: %s\n", e->e_dn, 0, 0);
29
30         /* nobody else can add until we lock our parent */
31         ldap_pvt_thread_mutex_lock(&li->li_add_mutex);
32
33         if ( ( bdb2i_dn2id( be, e->e_ndn ) ) != NOID ) {
34                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
35                 entry_free( e );
36                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
37                 return( -1 );
38         }
39
40         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
41                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
42
43                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
44                         0, 0, 0 );
45
46                 entry_free( e );
47                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
48                     "" );
49                 return( -1 );
50         }
51
52         /*
53          * Get the parent dn and see if the corresponding entry exists.
54          * If the parent does not exist, only allow the "root" user to
55          * add the entry.
56          */
57
58         if ( (pdn = dn_parent( be, e->e_ndn )) != NULL ) {
59                 char *matched = NULL;
60
61                 /* get parent with writer lock */
62                 if ( (p = bdb2i_dn2entry_w( be, pdn, &matched )) == NULL ) {
63                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
64                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
65                             0, 0 );
66                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
67                             matched, "" );
68
69                         if ( matched != NULL ) {
70                                 free( matched );
71                         }
72
73                         entry_free( e );
74                         free( pdn );
75                         return -1;
76                 }
77
78                 /* don't need the add lock anymore */
79                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
80
81                 free(pdn);
82
83                 if ( matched != NULL ) {
84                         free( matched );
85                 }
86
87                 if ( ! access_allowed( be, conn, op, p,
88                         "children", NULL, ACL_WRITE ) )
89                 {
90                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
91                             0, 0 );
92                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
93                             "", "" );
94
95                         /* free parent and writer lock */
96                         bdb2i_cache_return_entry_w( &li->li_cache, p ); 
97
98                         entry_free( e );
99                         return -1;
100                 }
101
102         } else {
103                 /* no parent, must be adding entry to root */
104                 if ( ! be_isroot( be, op->o_ndn ) ) {
105                         ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
106                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
107                             0, 0 );
108                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
109                             "", "" );
110
111                         entry_free( e );
112                         return -1;
113                 }
114
115                 /*
116                  * no parent, acquire the root write lock
117                  * and release the add lock.
118                  */
119                 ldap_pvt_thread_mutex_lock(&li->li_root_mutex);
120                 rootlock = 1;
121                 ldap_pvt_thread_mutex_unlock(&li->li_add_mutex);
122         }
123
124         /* acquire required reader/writer lock */
125         if (entry_rdwr_lock(e, 1)) {
126                 if( p != NULL) {
127                         /* free parent and writer lock */
128                         bdb2i_cache_return_entry_w( &li->li_cache, p ); 
129                 }
130
131                 if ( rootlock ) {
132                         /* release root lock */
133                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
134                 }
135
136                 Debug( LDAP_DEBUG_ANY, "add: could not lock entry\n",
137                         0, 0, 0 );
138
139                 entry_free(e);
140
141                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
142                 return( -1 );
143         }
144
145         e->e_id = bdb2i_next_id( be );
146
147         /*
148          * Try to add the entry to the cache, assign it a new dnid
149          * This should only fail if the entry already exists.
150          */
151
152         if ( bdb2i_cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
153                                                                 != 0 ) {
154                 if( p != NULL) {
155                         /* free parent and writer lock */
156                         bdb2i_cache_return_entry_w( &li->li_cache, p ); 
157                 }
158                 if ( rootlock ) {
159                         /* release root lock */
160                         ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
161                 }
162
163                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
164                     0 );
165                 bdb2i_next_id_return( be, e->e_id );
166                 
167                 entry_rdwr_unlock(e, 1);
168                 entry_free( e );
169
170                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
171                 return( -1 );
172         }
173
174         /*
175          * add it to the id2children index for the parent
176          */
177
178         if ( bdb2i_id2children_add( be, p, e ) != 0 ) {
179                 Debug( LDAP_DEBUG_TRACE, "bdb2i_id2children_add failed\n", 0,
180                     0, 0 );
181                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
182
183                 goto return_results;
184         }
185
186         /*
187          * Add the entry to the attribute indexes, then add it to
188          * the id2children index, dn2id index, and the id2entry index.
189          */
190
191         /* attribute indexes */
192         if ( bdb2i_index_add_entry( be, e ) != 0 ) {
193                 Debug( LDAP_DEBUG_TRACE, "bdb2i_index_add_entry failed\n", 0,
194                     0, 0 );
195                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
196
197                 goto return_results;
198         }
199
200         /* dn2id index */
201         if ( bdb2i_dn2id_add( be, e->e_ndn, e->e_id ) != 0 ) {
202                 Debug( LDAP_DEBUG_TRACE, "bdb2i_dn2id_add failed\n", 0,
203                     0, 0 );
204                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
205
206                 goto return_results;
207         }
208
209         /* id2entry index */
210         if ( bdb2i_id2entry_add( be, e ) != 0 ) {
211                 Debug( LDAP_DEBUG_TRACE, "bdb2i_id2entry_add failed\n", 0,
212                     0, 0 );
213                 (void) bdb2i_dn2id_delete( be, e->e_ndn );
214                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
215
216                 goto return_results;
217         }
218
219         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
220         rc = 0;
221
222 return_results:;
223         if (p != NULL) {
224                 /* free parent and writer lock */
225                 bdb2i_cache_return_entry_w( &li->li_cache, p ); 
226
227         }
228
229         if ( rootlock ) {
230                 /* release root lock */
231                 ldap_pvt_thread_mutex_unlock(&li->li_root_mutex);
232         }
233
234         bdb2i_cache_set_state( &li->li_cache, e, 0 );
235
236         /* free entry and writer lock */
237         bdb2i_cache_return_entry_w( &li->li_cache, e ); 
238
239         return( rc );
240 }
241
242
243 int
244 bdb2_back_add(
245     BackendDB   *be,
246     Connection  *conn,
247     Operation   *op,
248     Entry       *e
249 )
250 {
251         DB_LOCK  lock;
252         struct ldbminfo *li  = (struct ldbminfo *) be->be_private;
253
254         struct   timeval  time1, time2;
255         char     *elapsed_time;
256         int      ret;
257
258         gettimeofday( &time1, NULL );
259
260         if ( bdb2i_enter_backend_w( get_dbenv( be ), &lock ) != 0 ) {
261
262                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
263                 return( -1 );
264
265         }
266
267         /*  check, if a new default attribute index will be created,
268                 in which case we have to open the index file BEFORE TP  */
269         if ( ( slapMode == SLAP_SERVER_MODE ) || ( slapMode == SLAP_TOOL_MODE ) )
270                 bdb2i_check_default_attr_index_add( li, e );
271
272         ret = bdb2i_back_add_internal( be, conn, op, e );
273
274         (void) bdb2i_leave_backend( get_dbenv( be ), lock );
275
276         if ( bdb2i_do_timing ) {
277
278                 gettimeofday( &time2, NULL);
279                 elapsed_time = bdb2i_elapsed( time1, time2 );
280                 Debug( LDAP_DEBUG_ANY, "conn=%d op=%d ADD elapsed=%s\n",
281                                 conn->c_connid, op->o_opid, elapsed_time );
282                 free( elapsed_time );
283
284         }
285
286         return( ret );
287 }
288
289