1 /* ctxcsn.c -- Context CSN Management Routines */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2009 The OpenLDAP Foundation.
6 * Portions Copyright 2003 IBM Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
22 #include <ac/string.h>
23 #include <ac/socket.h>
27 #include "lutil_ldap.h"
29 const struct berval slap_ldapsync_bv = BER_BVC("ldapsync");
30 const struct berval slap_ldapsync_cn_bv = BER_BVC("cn=ldapsync");
33 /* maxcsn->bv_val must point to a char buf[LDAP_LUTIL_CSNSTR_BUFSIZE] */
37 struct berval *maxcsn,
41 struct slap_csn_entry *csne, *committed_csne = NULL;
42 BackendDB *be = op->o_bd->bd_self;
45 assert( maxcsn->bv_val != NULL );
46 assert( maxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE );
52 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
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;
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;
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,
75 maxcsn->bv_val[0] = 0;
78 ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
82 slap_rewind_commit_csn( Operation *op )
84 struct slap_csn_entry *csne;
85 BackendDB *be = op->o_bd->bd_self;
87 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
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;
96 ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
100 slap_graduate_commit_csn( Operation *op )
102 struct slap_csn_entry *csne;
105 if ( op == NULL ) return;
106 if ( op->o_bd == NULL ) return;
107 be = op->o_bd->bd_self;
109 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
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,
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 );
120 ch_free( csne->ce_csn.bv_val );
126 ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
131 static struct berval ocbva[] = {
134 BER_BVC("syncProviderSubentry"),
139 slap_create_context_csn_entry(
141 struct berval *context_csn )
149 attr_merge( e, slap_schema.si_ad_objectClass,
151 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
153 attr_merge_one( e, slap_schema.si_ad_cn,
154 (struct berval *)&slap_ldapsync_bv, NULL );
157 attr_merge_one( e, slap_schema.si_ad_contextCSN,
161 BER_BVSTR( &bv, "{}" );
162 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
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 );
176 struct slap_csn_entry *pending;
177 BackendDB *be = op->o_bd->bd_self;
179 pending = (struct slap_csn_entry *) ch_calloc( 1,
180 sizeof( struct slap_csn_entry ));
182 Debug( LDAP_DEBUG_SYNC, "slap_queue_csn: queing %p %s\n", csn->bv_val, csn->bv_val, 0 );
184 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
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 );
202 if ( csn == NULL ) return LDAP_OTHER;
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 );
208 slap_queue_csn( op, csn );
210 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );