]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
bcf7a4f687ce222ba3f03ea016f278cd059535b7
[openldap] / servers / slapd / ctxcsn.c
1 /* $OpenLDAP$ */
2 /*
3  * Context CSN Management Routines
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <db.h>
30
31 #include "ldap_pvt.h"
32 #include "lutil.h"
33 #include "slap.h"
34 #include "lutil_ldap.h"
35
36 struct berval *
37 slap_get_commit_csn( Operation *op )
38 {
39         struct berval *max_committed_csn = NULL;
40         struct slap_csn_entry *csne = NULL, *committed_csne = NULL;
41         int i = 0;
42
43         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
44
45         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
46                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
47                         break;
48         }
49
50         if ( csne ) {
51                 csne->state = SLAP_CSN_COMMIT;
52         }
53
54         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
55                 if ( csne->state == SLAP_CSN_COMMIT )
56                         committed_csne = csne;
57                 if ( csne->state == SLAP_CSN_PENDING )
58                         break;
59         }
60
61         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
62
63         if ( committed_csne ) {
64                 max_committed_csn = ber_dupbv( NULL, committed_csne->csn );
65         }
66
67         return max_committed_csn;
68 }
69
70 void
71 slap_rewind_commit_csn( Operation *op )
72 {
73         struct slap_csn_entry *csne = NULL;
74
75         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
76
77         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
78                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
79                         break;
80         }
81
82         if ( csne ) {
83                 csne->state = SLAP_CSN_PENDING;
84         }
85         
86         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
87 }
88
89 void
90 slap_graduate_commit_csn( Operation *op )
91 {
92         struct slap_csn_entry *csne = NULL;
93
94         ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
95
96         LDAP_TAILQ_FOREACH( csne, &op->o_bd->be_pending_csn_list, csn_link ) {
97                 if ( csne->opid == op->o_opid && csne->connid == op->o_connid )
98                         break;
99         }
100
101         if ( csne ) {
102                 LDAP_TAILQ_REMOVE( &op->o_bd->be_pending_csn_list, csne, csn_link );
103                 ch_free( csne->csn->bv_val );
104                 ch_free( csne->csn );
105                 ch_free( csne );
106         }
107
108         ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
109
110         return;
111 }
112
113 static struct berval ocbva[] = {
114         BER_BVC("top"),
115         BER_BVC("subentry"),
116         BER_BVC("syncProviderSubentry"),
117         {0,NULL}
118 };
119
120 Entry *
121 slap_create_context_csn_entry(
122         Backend *be,
123         struct berval *context_csn
124 )
125 {
126         Entry* e;
127         int rc;
128
129         struct berval bv;
130
131         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
132
133         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
134
135         bv.bv_val = "subentry";
136         bv.bv_len = sizeof("subentry")-1;
137
138         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &bv, NULL );
139
140         attr_merge_one( e, slap_schema.si_ad_cn, &slap_ldapsync_bv, NULL );
141
142         if ( context_csn ) {
143                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
144                         context_csn, NULL );
145         }
146
147         bv.bv_val = "{}";
148         bv.bv_len = sizeof("{}")-1;
149         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
150
151         build_new_dn( &e->e_name, &be->be_nsuffix[0], &slap_ldapsync_cn_bv );
152         ber_dupbv( &e->e_nname, &e->e_name );
153
154         return e;
155 }
156
157 static int
158 slap_contextcsn_callback(
159         Operation* op,
160         SlapReply* rs
161 )
162 {
163         if ( rs->sr_type != REP_SEARCH ) {
164                 *((int*)op->o_callback->sc_private) = 0;
165         } else {
166                 *((int*)op->o_callback->sc_private) = 1;
167         }
168         return LDAP_SUCCESS;
169 }
170
171 int
172 slap_get_csn(
173         Operation *op,
174         char *csnbuf,
175         int     len,
176         struct berval *csn,
177         int manage_ctxcsn
178 )
179 {
180         struct  slap_csn_entry *pending;
181
182         if ( manage_ctxcsn ) {
183                 pending = (struct slap_csn_entry *) ch_calloc( 1, sizeof( struct slap_csn_entry ));
184         }
185
186         if ( csn == NULL )
187                 return LDAP_OTHER;
188
189         csn->bv_len = lutil_csnstr( csnbuf, len, 0, 0 );
190         csn->bv_val = csnbuf;
191
192         if ( manage_ctxcsn ) {
193                 ldap_pvt_thread_mutex_lock( &op->o_bd->be_pcl_mutex );
194                 pending->csn = ber_dupbv( NULL, csn );
195                 pending->connid = op->o_connid;
196                 pending->opid = op->o_opid;
197                 pending->state = SLAP_CSN_PENDING;
198                 LDAP_TAILQ_INSERT_TAIL( &op->o_bd->be_pending_csn_list, pending, csn_link );
199                 ldap_pvt_thread_mutex_unlock( &op->o_bd->be_pcl_mutex );
200         }
201
202         return LDAP_SUCCESS;
203 }