ctxcsn.c 5.19 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
/* ctxcsn.c -- Context CSN Management Routines */
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
2
/* $OpenLDAP$ */
Kurt Zeilenga's avatar
Kurt Zeilenga committed
3
4
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
5
 * Copyright 2003-2009 The OpenLDAP Foundation.
Kurt Zeilenga's avatar
Kurt Zeilenga committed
6
7
 * Portions Copyright 2003 IBM Corporation.
 * All rights reserved.
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
8
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
9
10
11
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
12
 *
Kurt Zeilenga's avatar
Kurt Zeilenga committed
13
14
15
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
16
17
18
19
20
21
22
23
24
25
26
27
28
 */

#include "portable.h"

#include <stdio.h>

#include <ac/string.h>
#include <ac/socket.h>

#include "lutil.h"
#include "slap.h"
#include "lutil_ldap.h"

Howard Chu's avatar
Howard Chu committed
29
30
const struct berval slap_ldapsync_bv = BER_BVC("ldapsync");
const struct berval slap_ldapsync_cn_bv = BER_BVC("cn=ldapsync");
31
int slap_serverID;
Howard Chu's avatar
Howard Chu committed
32

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
33
/* maxcsn->bv_val must point to a char buf[LDAP_LUTIL_CSNSTR_BUFSIZE] */
Howard Chu's avatar
Howard Chu committed
34
void
35
36
slap_get_commit_csn(
	Operation *op,
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
37
38
	struct berval *maxcsn,
	int *foundit
39
)
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
40
{
41
	struct slap_csn_entry *csne, *committed_csne = NULL;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
42
	BackendDB *be = op->o_bd->bd_self;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
43

44
	if ( maxcsn ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
45
46
		assert( maxcsn->bv_val != NULL );
		assert( maxcsn->bv_len >= LDAP_LUTIL_CSNSTR_BUFSIZE );
47
	}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
48
49
50
	if ( foundit ) {
		*foundit = 0;
	}
Howard Chu's avatar
Howard Chu committed
51

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
52
	ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
53

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
54
	LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
55
56
		if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
			csne->ce_state = SLAP_CSN_COMMIT;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
57
			if ( foundit ) *foundit = 1;
58
59
			break;
		}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
60
61
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
62
	LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
63
64
		if ( csne->ce_state == SLAP_CSN_COMMIT ) committed_csne = csne;
		if ( csne->ce_state == SLAP_CSN_PENDING ) break;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
65
	}
66

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
67
68
69
70
71
72
73
74
75
76
77
	if ( maxcsn ) {
		if ( committed_csne ) {
			if ( committed_csne->ce_csn.bv_len < maxcsn->bv_len )
				maxcsn->bv_len = committed_csne->ce_csn.bv_len;
			AC_MEMCPY( maxcsn->bv_val, committed_csne->ce_csn.bv_val,
				maxcsn->bv_len+1 );
		} else {
			maxcsn->bv_len = 0;
			maxcsn->bv_val[0] = 0;
		}
	}
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
78
	ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
79
80
81
}

void
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
82
slap_rewind_commit_csn( Operation *op )
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
83
{
84
	struct slap_csn_entry *csne;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
85
	BackendDB *be = op->o_bd->bd_self;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
86

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
87
	ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
88

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
89
	LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
90
91
		if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
			csne->ce_state = SLAP_CSN_PENDING;
92
93
			break;
		}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
94
95
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
96
	ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
97
98
99
}

void
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
100
slap_graduate_commit_csn( Operation *op )
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
101
{
102
	struct slap_csn_entry *csne;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
103
	BackendDB *be;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
104

Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
105
106
	if ( op == NULL ) return;
	if ( op->o_bd == NULL ) return;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
107
	be = op->o_bd->bd_self;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
108

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
109
	ldap_pvt_thread_mutex_lock( &be->be_pcl_mutex );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
110

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
111
	LDAP_TAILQ_FOREACH( csne, be->be_pending_csn_list, ce_csn_link ) {
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
112
		if ( csne->ce_opid == op->o_opid && csne->ce_connid == op->o_connid ) {
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
113
			LDAP_TAILQ_REMOVE( be->be_pending_csn_list,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
114
				csne, ce_csn_link );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
115
116
			Debug( LDAP_DEBUG_SYNC, "slap_graduate_commit_csn: removing %p %s\n",
				csne->ce_csn.bv_val, csne->ce_csn.bv_val, 0 );
Howard Chu's avatar
Howard Chu committed
117
118
119
			if ( op->o_csn.bv_val == csne->ce_csn.bv_val ) {
				BER_BVZERO( &op->o_csn );
			}
120
			ch_free( csne->ce_csn.bv_val );
121
122
123
			ch_free( csne );
			break;
		}
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
124
125
	}

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
126
	ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
127
128
129
130

	return;
}

131
132
133
134
static struct berval ocbva[] = {
	BER_BVC("top"),
	BER_BVC("subentry"),
	BER_BVC("syncProviderSubentry"),
Pierangelo Masarati's avatar
Pierangelo Masarati committed
135
	BER_BVNULL
136
137
};

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
138
Entry *
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
139
slap_create_context_csn_entry(
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
140
	Backend *be,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
141
	struct berval *context_csn )
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
142
143
144
{
	Entry* e;

145
	struct berval bv;
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
146

147
	e = entry_alloc();
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
148

Kurt Zeilenga's avatar
Kurt Zeilenga committed
149
150
151
152
153
154
	attr_merge( e, slap_schema.si_ad_objectClass,
		ocbva, NULL );
	attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
		&ocbva[1], NULL );
	attr_merge_one( e, slap_schema.si_ad_cn,
		(struct berval *)&slap_ldapsync_bv, NULL );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
155

156
157
158
	if ( context_csn ) {
		attr_merge_one( e, slap_schema.si_ad_contextCSN,
			context_csn, NULL );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
159
160
	}

Pierangelo Masarati's avatar
Pierangelo Masarati committed
161
	BER_BVSTR( &bv, "{}" );
162
163
	attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );

Kurt Zeilenga's avatar
Kurt Zeilenga committed
164
165
	build_new_dn( &e->e_name, &be->be_nsuffix[0],
		(struct berval *)&slap_ldapsync_cn_bv, NULL );
Howard Chu's avatar
Howard Chu committed
166
	ber_dupbv( &e->e_nname, &e->e_name );
167

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
168
169
170
	return e;
}

171
172
173
174
175
176
void
slap_queue_csn(
	Operation *op,
	struct berval *csn )
{
	struct slap_csn_entry *pending;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
177
	BackendDB *be = op->o_bd->bd_self;
178
179
180

	pending = (struct slap_csn_entry *) ch_calloc( 1,
			sizeof( struct slap_csn_entry ));
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
181
182
183

	Debug( LDAP_DEBUG_SYNC, "slap_queue_csn: queing %p %s\n", csn->bv_val, csn->bv_val, 0 );

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
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
189
190
	pending->ce_connid = op->o_connid;
	pending->ce_opid = op->o_opid;
	pending->ce_state = SLAP_CSN_PENDING;
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
191
	LDAP_TAILQ_INSERT_TAIL( be->be_pending_csn_list,
192
		pending, ce_csn_link );
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
193
	ldap_pvt_thread_mutex_unlock( &be->be_pcl_mutex );
194
195
}

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
196
197
198
199
int
slap_get_csn(
	Operation *op,
	struct berval *csn,
Kurt Zeilenga's avatar
cleanup    
Kurt Zeilenga committed
200
	int manage_ctxcsn )
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
201
{
Kurt Zeilenga's avatar
Kurt Zeilenga committed
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
208
	if ( manage_ctxcsn )
		slap_queue_csn( op, csn );
Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
209

Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
210
211
	ldap_pvt_thread_mutex_unlock( &gmtime_mutex );

Jong Hyuk Choi's avatar
Jong Hyuk Choi committed
212
213
	return LDAP_SUCCESS;
}