]> git.sur5r.net Git - openldap/blob - servers/slapd/ctxcsn.c
fix ITS#5959 fix
[openldap] / servers / slapd / ctxcsn.c
1 /* ctxcsn.c -- Context CSN Management Routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2009 The OpenLDAP Foundation.
6  * Portions Copyright 2003 IBM Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/string.h>
23 #include <ac/socket.h>
24
25 #include "lutil.h"
26 #include "slap.h"
27 #include "lutil_ldap.h"
28
29 const struct berval slap_ldapsync_bv = BER_BVC("ldapsync");
30 const struct berval slap_ldapsync_cn_bv = BER_BVC("cn=ldapsync");
31 int slap_serverID;
32
33 /* maxcsn->bv_val must point to a char buf[LDAP_LUTIL_CSNSTR_BUFSIZE] */
34 void
35 slap_get_commit_csn(
36         Operation *op,
37         struct berval *maxcsn,
38         int *foundit
39 )
40 {
41         struct slap_csn_entry *csne, *committed_csne = NULL;
42         BackendDB *be = op->o_bd->bd_self;
43
44         if ( maxcsn ) {
45                 assert( maxcsn->bv_val != NULL );
46                 assert( maxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE );
47         }
48         if ( foundit ) {
49                 *foundit = 0;
50         }
51
52         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
53
54         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
55                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
56                         csne->ce_state = SLAP_CSN_COMMIT;
57                         if ( foundit ) *foundit = 1;
58                         break;
59                 }
60         }
61
62         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
63                 if ( csne->ce_state == SLAP_CSN_COMMIT ) committed_csne = csne;
64                 if ( csne->ce_state == SLAP_CSN_PENDING ) break;
65         }
66
67         if ( maxcsn ) {
68                 if ( committed_csne ) {
69                         if ( committed_csne->ce_csn.bv_len < maxcsn->bv_len )
70                                 maxcsn->bv_len = committed_csne->ce_csn.bv_len;
71                         AC_MEMCPY( maxcsn->bv_val, committed_csne->ce_csn.bv_val,
72                                 maxcsn->bv_len+1 );
73                 } else {
74                         maxcsn->bv_len = 0;
75                         maxcsn->bv_val[0] = 0;
76                 }
77         }
78         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
79 }
80
81 void
82 slap_rewind_commit_csn( Operation *op )
83 {
84         struct slap_csn_entry *csne;
85         BackendDB *be = op->o_bd->bd_self;
86
87         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
88
89         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
90                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
91                         csne->ce_state = SLAP_CSN_PENDING;
92                         break;
93                 }
94         }
95
96         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
97 }
98
99 void
100 slap_graduate_commit_csn( Operation *op )
101 {
102         struct slap_csn_entry *csne;
103         BackendDB *be;
104
105         if ( op == NULL ) return;
106         if ( op->o_bd == NULL ) return;
107         be = op->o_bd->bd_self;
108
109         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
110
111         LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
112                 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
113                         LDAP_TAILQ_REMOVE( be->be_pending_csn_list,
114                                 csne, ce_csn_link );
115                         Debug( LDAP_DEBUG_SYNC, "slap_graduate_commit_csn: removing %p %s\n",
116                                 csne->ce_csn.bv_val, csne->ce_csn.bv_val, 0 );
117                         if ( op->o_csn.bv_val == csne->ce_csn.bv_val ) {
118                                 BER_BVZERO( &op->o_csn );
119                         }
120                         ch_free( csne->ce_csn.bv_val );
121                         ch_free( csne );
122                         break;
123                 }
124         }
125
126         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
127
128         return;
129 }
130
131 static struct berval ocbva[] = {
132         BER_BVC("top"),
133         BER_BVC("subentry"),
134         BER_BVC("syncProviderSubentry"),
135         BER_BVNULL
136 };
137
138 Entry *
139 slap_create_context_csn_entry(
140         Backend *be,
141         struct berval *context_csn )
142 {
143         Entry* e;
144
145         struct berval bv;
146
147         e = entry_alloc();
148
149         attr_merge( e, slap_schema.si_ad_objectClass,
150                 ocbva, NULL );
151         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
152                 &ocbva[1], NULL );
153         attr_merge_one( e, slap_schema.si_ad_cn,
154                 (struct berval *)&slap_ldapsync_bv, NULL );
155
156         if ( context_csn ) {
157                 attr_merge_one( e, slap_schema.si_ad_contextCSN,
158                         context_csn, NULL );
159         }
160
161         BER_BVSTR( &bv, "{}" );
162         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
163
164         build_new_dn( &e->e_name, &be->be_nsuffix[0],
165                 (struct berval *)&slap_ldapsync_cn_bv, NULL );
166         ber_dupbv( &e->e_nname, &e->e_name );
167
168         return e;
169 }
170
171 void
172 slap_queue_csn(
173         Operation *op,
174         struct berval *csn )
175 {
176         struct slap_csn_entry *pending;
177         BackendDB *be = op->o_bd->bd_self;
178
179         pending = (struct slap_csn_entry *) ch_calloc( 1,
180                         sizeof( struct slap_csn_entry ));
181
182         Debug( LDAP_DEBUG_SYNC, "slap_queue_csn: queing %p %s\n", csn->bv_val, csn->bv_val, 0 );
183
184         ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
185
186         ber_dupbv( &pending->ce_csn, csn );
187         ber_bvreplace_x( &op->o_csn, &pending->ce_csn, op->o_tmpmemctx );
188         pending->ce_connid = op->o_connid;
189         pending->ce_opid = op->o_opid;
190         pending->ce_state = SLAP_CSN_PENDING;
191         LDAP_TAILQ_INSERT_TAIL( be->be_pending_csn_list,
192                 pending, ce_csn_link );
193         ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
194 }
195
196 int
197 slap_get_csn(
198         Operation *op,
199         struct berval *csn,
200         int manage_ctxcsn )
201 {
202         if ( csn == NULL ) return LDAP_OTHER;
203
204         /* gmtime doesn't always need a mutex, but lutil_csnstr does */
205         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
206         csn->bv_len = lutil_csnstr( csn->bv_val, csn->bv_len, slap_serverID, 0 );
207         if ( manage_ctxcsn )
208                 slap_queue_csn( op, csn );
209
210         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
211
212         return LDAP_SUCCESS;
213 }