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"
31 /* maxcsn->bv_val must point to a char buf[LDAP_LUTIL_CSNSTR_BUFSIZE] */
35 struct berval *maxcsn,
39 struct slap_csn_entry *csne, *committed_csne = NULL;
40 BackendDB *be = op->o_bd->bd_self;
44 assert( maxcsn->bv_val != NULL );
45 assert( maxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE );
51 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
53 if ( !BER_BVISEMPTY( &op->o_csn )) {
54 sid = slap_parse_csn_sid( &op->o_csn );
57 LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
58 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
59 csne->ce_state = SLAP_CSN_COMMIT;
60 if ( foundit ) *foundit = 1;
65 LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
66 if ( sid != -1 && sid == csne->ce_sid ) {
67 if ( csne->ce_state == SLAP_CSN_COMMIT ) committed_csne = csne;
68 if ( csne->ce_state == SLAP_CSN_PENDING ) break;
73 if ( committed_csne ) {
74 if ( committed_csne->ce_csn.bv_len < maxcsn->bv_len )
75 maxcsn->bv_len = committed_csne->ce_csn.bv_len;
76 AC_MEMCPY( maxcsn->bv_val, committed_csne->ce_csn.bv_val,
80 maxcsn->bv_val[0] = 0;
83 ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
87 slap_rewind_commit_csn( Operation *op )
89 struct slap_csn_entry *csne;
90 BackendDB *be = op->o_bd->bd_self;
92 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
94 LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
95 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
96 csne->ce_state = SLAP_CSN_PENDING;
101 ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
105 slap_graduate_commit_csn( Operation *op )
107 struct slap_csn_entry *csne;
110 if ( op == NULL ) return;
111 if ( op->o_bd == NULL ) return;
112 be = op->o_bd->bd_self;
114 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
116 LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
117 if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
118 LDAP_TAILQ_REMOVE( be->be_pending_csn_list,
120 Debug( LDAP_DEBUG_SYNC, "slap_graduate_commit_csn: removing %p %s\n",
121 csne->ce_csn.bv_val, csne->ce_csn.bv_val, 0 );
122 if ( op->o_csn.bv_val == csne->ce_csn.bv_val ) {
123 BER_BVZERO( &op->o_csn );
125 ch_free( csne->ce_csn.bv_val );
131 ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
141 struct slap_csn_entry *pending;
142 BackendDB *be = op->o_bd->bd_self;
144 pending = (struct slap_csn_entry *) ch_calloc( 1,
145 sizeof( struct slap_csn_entry ));
147 Debug( LDAP_DEBUG_SYNC, "slap_queue_csn: queing %p %s\n", csn->bv_val, csn->bv_val, 0 );
149 ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
151 ber_dupbv( &pending->ce_csn, csn );
152 ber_bvreplace_x( &op->o_csn, &pending->ce_csn, op->o_tmpmemctx );
153 pending->ce_sid = slap_parse_csn_sid( csn );
154 pending->ce_connid = op->o_connid;
155 pending->ce_opid = op->o_opid;
156 pending->ce_state = SLAP_CSN_PENDING;
157 LDAP_TAILQ_INSERT_TAIL( be->be_pending_csn_list,
158 pending, ce_csn_link );
159 ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
168 if ( csn == NULL ) return LDAP_OTHER;
170 /* gmtime doesn't always need a mutex, but lutil_csnstr does */
171 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
172 csn->bv_len = lutil_csnstr( csn->bv_val, csn->bv_len, slap_serverID, 0 );
174 slap_queue_csn( op, csn );
176 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );