]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Multi-threaded slapindex
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2005 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
21 #include <ac/socket.h>
22 #include <ac/string.h>
23
24 #include "slap.h"
25 #include "back-ldbm.h"
26 #include "proto-back-ldbm.h"
27
28 static int
29 ldbm_csn_cb(
30         Operation *op,
31         SlapReply *rs )
32 {
33         op->o_callback = op->o_callback->sc_next;
34         slap_graduate_commit_csn( op );
35         return SLAP_CB_CONTINUE;
36 }
37
38 int
39 ldbm_back_add(
40     Operation   *op,
41     SlapReply   *rs )
42 {
43         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
44         struct berval   pdn;
45         Entry           *p = NULL;
46         ID               id = NOID;
47         AttributeDescription *children = slap_schema.si_ad_children;
48         AttributeDescription *entry = slap_schema.si_ad_entry;
49         char textbuf[SLAP_TEXT_BUFLEN];
50         size_t textlen = sizeof textbuf;
51         slap_callback cb = { NULL };
52 #ifdef LDBM_SUBENTRIES
53         int subentry;
54 #endif
55
56         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n",
57                 op->o_req_dn.bv_val, 0, 0);
58         
59         slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
60
61         cb.sc_cleanup = ldbm_csn_cb;
62         cb.sc_next = op->o_callback;
63         op->o_callback = &cb;
64
65         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
66                 get_manageDIT(op), &rs->sr_text, textbuf, textlen );
67
68         if ( rs->sr_err != LDAP_SUCCESS ) {
69                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
70                         rs->sr_text, 0, 0 );
71
72                 send_ldap_result( op, rs );
73                 return rs->sr_err;
74         }
75         rs->sr_text = NULL;
76
77 #ifdef LDBM_SUBENTRIES
78         subentry = is_entry_subentry( op->oq_add.rs_e );
79 #endif
80
81         if ( !access_allowed( op, op->oq_add.rs_e,
82                                 entry, NULL, ACL_WADD, NULL ) )
83         {
84                 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
85                     0, 0 );
86
87                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
88                     "no write access to entry" );
89
90                 return LDAP_INSUFFICIENT_ACCESS;
91         }
92
93         /* grab giant lock for writing */
94         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
95
96         rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id );
97         if ( rs->sr_err || id != NOID ) {
98                 /* if (rs->sr_err) something bad happened to ldbm cache */
99                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
100                 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
101                 send_ldap_result( op, rs );
102                 return rs->sr_err;
103         }
104
105         /*
106          * Get the parent dn and see if the corresponding entry exists.
107          * If the parent does not exist, only allow the "root" user to
108          * add the entry.
109          */
110
111         if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
112                 pdn = slap_empty_bv;
113         } else {
114                 dnParent( &op->o_req_ndn, &pdn );
115         }
116
117         if( pdn.bv_len ) {
118                 Entry *matched = NULL;
119
120                 /* get parent with writer lock */
121                 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
122                         if ( matched != NULL ) {
123                                 rs->sr_matched = ch_strdup( matched->e_dn );
124                                 rs->sr_ref = is_entry_referral( matched )
125                                         ? get_entry_referrals( op, matched )
126                                         : NULL;
127                                 cache_return_entry_r( &li->li_cache, matched );
128
129                         } else {
130                                 rs->sr_ref = referral_rewrite( default_referral,
131                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
132                         }
133
134                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
135
136                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
137                                 0, 0, 0 );
138
139                         rs->sr_text = rs->sr_ref
140                                 ? "parent is referral" : "parent does not exist";
141                         rs->sr_err = LDAP_REFERRAL;
142                         send_ldap_result( op, rs );
143
144                         ber_bvarray_free( rs->sr_ref );
145                         free( (char *)rs->sr_matched );
146                         rs->sr_ref = NULL;
147                         rs->sr_matched = NULL;
148                         return rs->sr_err;
149                 }
150
151                 if ( ! access_allowed( op, p, children, NULL, ACL_WADD, NULL ) ) {
152                         /* free parent and writer lock */
153                         cache_return_entry_w( &li->li_cache, p ); 
154                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
155
156                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
157                             0, 0 );
158
159                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
160                             "no write access to parent" );
161
162                         return LDAP_INSUFFICIENT_ACCESS;
163                 }
164
165 #ifdef LDBM_SUBENTRIES
166                 if ( is_entry_subentry( p )) {
167                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
168                                 0, 0, 0 );
169                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
170                         rs->sr_text = "parent is a subentry";
171                         goto return_results;
172                 }
173 #endif
174
175                 if ( is_entry_alias( p ) ) {
176                         /* parent is an alias, don't allow add */
177
178                         /* free parent and writer lock */
179                         cache_return_entry_w( &li->li_cache, p );
180                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
181
182                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
183                             0, 0 );
184
185                         send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
186                             "parent is an alias" );
187
188                         return LDAP_ALIAS_PROBLEM;
189                 }
190
191                 if ( is_entry_referral( p ) ) {
192                         /* parent is a referral, don't allow add */
193                         rs->sr_matched = ch_strdup( p->e_dn );
194                         rs->sr_ref = is_entry_referral( p )
195                                 ? get_entry_referrals( op, p )
196                                 : NULL;
197
198                         /* free parent and writer lock */
199                         cache_return_entry_w( &li->li_cache, p );
200                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
201
202                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
203                             0, 0 );
204                         rs->sr_err = LDAP_REFERRAL;
205                         send_ldap_result( op, rs );
206
207                         ber_bvarray_free( rs->sr_ref );
208                         free( (char *)rs->sr_matched );
209                         rs->sr_ref = NULL;
210                         rs->sr_matched = NULL;
211                         return rs->sr_err;
212                 }
213
214 #ifdef LDBM_SUBENTRIES
215                 if ( subentry ) {
216                         /* FIXME: */
217                         /* parent must be an administrative point of the required kind */
218                 }
219 #endif
220
221         } else {
222                 assert( pdn.bv_val == NULL || *pdn.bv_val == '\0' );
223
224                 if (( !be_isroot(op) && !be_shadow_update(op) )
225                         && !is_entry_glue( op->oq_add.rs_e ))
226                 {
227                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
228
229                         Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
230                                 pdn.bv_val == NULL ? "suffix" : "entry at root", 0, 0 );
231
232                         send_ldap_error( op, rs, LDAP_NO_SUCH_OBJECT, NULL );
233                         return LDAP_NO_SUCH_OBJECT;
234                 }
235         }
236
237         if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
238                 if( p != NULL) {
239                         /* free parent and writer lock */
240                         cache_return_entry_w( &li->li_cache, p ); 
241                 }
242
243                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
244
245                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
246                         0, 0, 0 );
247
248                 send_ldap_error( op, rs, LDAP_OTHER,
249                         "next_id add failed" );
250
251                 return LDAP_OTHER;
252         }
253
254         /*
255          * Try to add the entry to the cache, assign it a new dnid.
256          */
257         rs->sr_err = cache_add_entry_rw( &li->li_cache, op->oq_add.rs_e,
258                 CACHE_WRITE_LOCK );
259
260         if ( rs->sr_err != 0 ) {
261                 if( p != NULL) {
262                         /* free parent and writer lock */
263                         cache_return_entry_w( &li->li_cache, p ); 
264                 }
265
266                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
267
268                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
269                     0 );
270
271                 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
272                 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
273                 send_ldap_result( op, rs );
274
275                 return rs->sr_err;
276         }
277
278         rs->sr_err = -1;
279
280         /* attribute indexes */
281         if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
282                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
283                     0, 0 );
284                 
285                 send_ldap_error( op, rs, LDAP_OTHER,
286                         "index generation failed" );
287
288                 goto return_results;
289         }
290
291         /* dn2id index */
292         if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname,
293                 op->oq_add.rs_e->e_id ) != 0 )
294         {
295                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
296                     0, 0 );
297                 /* FIXME: delete attr indices? */
298
299                 send_ldap_error( op, rs, LDAP_OTHER,
300                         "DN index generation failed" );
301
302                 goto return_results;
303         }
304
305         /* id2entry index */
306         if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
307                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
308                     0, 0 );
309
310                 /* FIXME: delete attr indices? */
311                 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname,
312                         op->oq_add.rs_e->e_id );
313                 
314                 send_ldap_error( op, rs, LDAP_OTHER,
315                         "entry store failed" );
316
317                 goto return_results;
318         }
319
320         rs->sr_err = LDAP_SUCCESS;
321         rs->sr_text = NULL;
322         send_ldap_result( op, rs );
323
324         /* marks the entry as committed, so it is added to the cache;
325          * otherwise it is removed from the cache, but not destroyed;
326          * it will be destroyed by the caller */
327         cache_entry_commit( op->oq_add.rs_e );
328
329 return_results:;
330         if (p != NULL) {
331                 /* free parent and writer lock */
332                 cache_return_entry_w( &li->li_cache, p ); 
333         }
334
335         if ( rs->sr_err ) {
336                 /*
337                  * in case of error, writer lock is freed 
338                  * and entry's private data is destroyed.
339                  * otherwise, this is done when entry is released
340                  */
341                 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
342                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
343         }
344
345         return( rs->sr_err );
346 }