]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Import root_mutex release change from devel.
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm 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-ldbm.h"
12 #include "proto-back-ldbm.h"
13
14 int
15 ldbm_back_add(
16     Backend     *be,
17     Connection  *conn,
18     Operation   *op,
19     Entry       *e
20 )
21 {
22         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
23         char            *dn, *pdn;
24         Entry           *p = NULL;
25         int                     rootlock = 0;
26         int                     rc = -1; 
27
28         dn = e->e_ndn;
29
30         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
31
32         /* nobody else can add until we lock our parent */
33         pthread_mutex_lock(&li->li_add_mutex);
34
35         if ( ( dn2id( be, dn ) ) != NOID ) {
36                 pthread_mutex_unlock(&li->li_add_mutex);
37                 entry_free( e );
38                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
39                 return( -1 );
40         }
41
42         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
43                 pthread_mutex_unlock(&li->li_add_mutex);
44
45                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
46                         0, 0, 0 );
47
48                 entry_free( e );
49                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
50                     "" );
51                 return( -1 );
52         }
53
54         /*
55          * Get the parent dn and see if the corresponding entry exists.
56          * If the parent does not exist, only allow the "root" user to
57          * add the entry.
58          */
59
60         if ( (pdn = dn_parent( be, dn )) != NULL ) {
61                 char *matched = NULL;
62
63                 /* get parent with writer lock */
64                 if ( (p = dn2entry_w( be, pdn, &matched )) == NULL ) {
65                         pthread_mutex_unlock(&li->li_add_mutex);
66                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
67                             0, 0 );
68                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
69                             matched, "" );
70
71                         if ( matched != NULL ) {
72                                 free( matched );
73                         }
74
75                         entry_free( e );
76                         free( pdn );
77                         return -1;
78                 }
79
80                 /* don't need the add lock anymore */
81                 pthread_mutex_unlock(&li->li_add_mutex);
82
83                 free(pdn);
84
85                 if ( matched != NULL ) {
86                         free( matched );
87                 }
88
89                 if ( ! access_allowed( be, conn, op, p,
90                         "children", NULL, ACL_WRITE ) )
91                 {
92                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
93                             0, 0 );
94                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
95                             "", "" );
96
97                         /* free parent and writer lock */
98                         cache_return_entry_w( &li->li_cache, p ); 
99
100                         entry_free( e );
101                         return -1;
102                 }
103
104         } else {
105                 /* no parent, must be adding entry to root */
106                 if ( ! be_isroot( be, op->o_ndn ) ) {
107                         pthread_mutex_unlock(&li->li_add_mutex);
108                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
109                             0, 0 );
110                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
111                             "", "" );
112
113                         entry_free( e );
114                         return -1;
115                 }
116
117                 /*
118                  * no parent, acquire the root write lock
119                  * and release the add lock.
120                  */
121                 pthread_mutex_lock(&li->li_root_mutex);
122                 rootlock = 1;
123                 pthread_mutex_unlock(&li->li_add_mutex);
124         }
125
126         /*
127          * Try to add the entry to the cache, assign it a new dnid
128          * and mark it locked.  This should only fail if the entry
129          * already exists.
130          */
131
132         e->e_id = next_id( be );
133         if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING ) != 0 ) {
134                 if( p != NULL) {
135                         /* free parent and writer lock */
136                         cache_return_entry_w( &li->li_cache, p ); 
137                 }
138                 if ( rootlock ) {
139                         /* release root lock */
140                         pthread_mutex_unlock(&li->li_root_mutex);
141                 }
142
143                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
144                     0 );
145                 next_id_return( be, e->e_id );
146                 
147                 /* XXX this should be ok, no other thread should have access
148                  * because e hasn't been added to the cache yet
149                  */
150                 entry_free( e );
151                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
152                 return( -1 );
153         }
154
155         /* acquire writer lock */
156         entry_rdwr_lock(e, 1);
157
158         /*
159          * add it to the id2children index for the parent
160          */
161
162         if ( id2children_add( be, p, e ) != 0 ) {
163                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
164                     0, 0 );
165                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
166
167                 goto return_results;
168         }
169
170         /*
171          * Add the entry to the attribute indexes, then add it to
172          * the id2children index, dn2id index, and the id2entry index.
173          */
174
175         /* attribute indexes */
176         if ( index_add_entry( be, e ) != 0 ) {
177                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
178                     0, 0 );
179                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
180
181                 goto return_results;
182         }
183
184         /* dn2id index */
185         if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
186                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
187                     0, 0 );
188                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
189
190                 goto return_results;
191         }
192
193         /* id2entry index */
194         if ( id2entry_add( be, e ) != 0 ) {
195                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
196                     0, 0 );
197                 (void) dn2id_delete( be, dn );
198                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
199
200                 goto return_results;
201         }
202
203         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
204         rc = 0;
205
206 return_results:;
207         cache_set_state( &li->li_cache, e, 0 );
208
209         if (p != NULL) {
210                 /* free parent and writer lock */
211                 cache_return_entry_w( &li->li_cache, p ); 
212         }
213
214         if ( rootlock ) {
215                 /* release root lock */
216                 pthread_mutex_unlock(&li->li_root_mutex);
217         }
218
219         /* free entry and writer lock */
220         cache_return_entry_w( &li->li_cache, e ); 
221
222         return( rc );
223 }