seqmod.c 4.75 KB
Newer Older
1
2
3
/* seqmod.c - sequenced modifies */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
Quanah Gibson-Mount's avatar
Quanah Gibson-Mount committed
4
 * Copyright 2004-2020 The OpenLDAP Foundation.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * 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>.
 */
/* ACKNOWLEDGEMENTS:
 * This work was initially developed by Howard Chu for inclusion in
 * OpenLDAP Software.
 */

#include "portable.h"

#ifdef SLAPD_OVER_SEQMOD

#include "slap.h"
25
#include "config.h"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/* This overlay serializes concurrent attempts to modify a single entry */

typedef struct modtarget {
	struct modtarget *mt_next;
	struct modtarget *mt_tail;
	Operation *mt_op;
} modtarget;

typedef struct seqmod_info {
	Avlnode		*sm_mods;	/* entries being modified */
	ldap_pvt_thread_mutex_t	sm_mutex;
} seqmod_info;

static int
sm_avl_cmp( const void *c1, const void *c2 )
{
	const modtarget *m1, *m2;
	int rc;

	m1 = c1; m2 = c2;
	rc = m1->mt_op->o_req_ndn.bv_len - m2->mt_op->o_req_ndn.bv_len;

	if ( rc ) return rc;
	return ber_bvcmp( &m1->mt_op->o_req_ndn, &m2->mt_op->o_req_ndn );
}

static int
seqmod_op_cleanup( Operation *op, SlapReply *rs )
{
Howard Chu's avatar
Howard Chu committed
56
57
	slap_callback *sc = op->o_callback;
	seqmod_info *sm = sc->sc_private;
Howard Chu's avatar
Howard Chu committed
58
	modtarget *mt, mtdummy;
59
60
	Avlnode	 *av;

Howard Chu's avatar
Howard Chu committed
61
	mtdummy.mt_op = op;
62
63
	/* This op is done, remove it */
	ldap_pvt_thread_mutex_lock( &sm->sm_mutex );
Howard Chu's avatar
Howard Chu committed
64
	av = avl_find2( sm->sm_mods, &mtdummy, sm_avl_cmp );
65
	assert(av != NULL);
66
67
68
69
70
71
72
73
74
75
76

	mt = av->avl_data;

	/* If there are more, promote the next one */
	if ( mt->mt_next ) {
		av->avl_data = mt->mt_next;
		mt->mt_next->mt_tail = mt->mt_tail;
	} else {
		avl_delete( &sm->sm_mods, mt, sm_avl_cmp );
	}
	ldap_pvt_thread_mutex_unlock( &sm->sm_mutex );
Howard Chu's avatar
Howard Chu committed
77
78
	op->o_callback = sc->sc_next;
	op->o_tmpfree( sc, op->o_tmpmemctx );
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

	return 0;
}

static int
seqmod_op_mod( Operation *op, SlapReply *rs )
{
	slap_overinst		*on = (slap_overinst *)op->o_bd->bd_info;
	seqmod_info		*sm = on->on_bi.bi_private;
	modtarget	*mt;
	Avlnode	*av;
	slap_callback *cb;

	cb = op->o_tmpcalloc( 1, sizeof(slap_callback) + sizeof(modtarget),
		op->o_tmpmemctx );
	mt = (modtarget *)(cb+1);
	mt->mt_next = NULL;
	mt->mt_tail = mt;
	mt->mt_op = op;

	/* See if we're already modifying this entry - don't allow
	 * near-simultaneous mods of the same entry
	 */
	ldap_pvt_thread_mutex_lock( &sm->sm_mutex );
Howard Chu's avatar
Howard Chu committed
103
	av = avl_find2( sm->sm_mods, mt, sm_avl_cmp );
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
	if ( av ) {
		modtarget *mtp = av->avl_data;
		mtp->mt_tail->mt_next = mt;
		mtp->mt_tail = mt;
		/* Wait for this op to get to head of list */
		while ( mtp != mt ) {
			ldap_pvt_thread_mutex_unlock( &sm->sm_mutex );
			ldap_pvt_thread_yield();
			/* Let it finish - should use a condition
			 * variable here... */
			ldap_pvt_thread_mutex_lock( &sm->sm_mutex );
			mtp = av->avl_data;
		}
	} else {
		/* Record that we're modifying this now */
		avl_insert( &sm->sm_mods, mt, sm_avl_cmp, avl_dup_error );
	}
	ldap_pvt_thread_mutex_unlock( &sm->sm_mutex );

	cb->sc_cleanup = seqmod_op_cleanup;
	cb->sc_private = sm;
	cb->sc_next = op->o_callback;
	op->o_callback = cb;

	return SLAP_CB_CONTINUE;
}

static int
seqmod_op_extended(
	Operation *op,
	SlapReply *rs
)
{
	if ( exop_is_write( op )) return seqmod_op_mod( op, rs );
	else return SLAP_CB_CONTINUE;
}

static int
seqmod_db_open(
143
	BackendDB *be,
144
	ConfigReply *cr
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
)
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	seqmod_info	*sm;

	sm = ch_calloc(1, sizeof(seqmod_info));
	on->on_bi.bi_private = sm;

	ldap_pvt_thread_mutex_init( &sm->sm_mutex );

	return 0;
}

static int
seqmod_db_close(
160
	BackendDB *be,
161
	ConfigReply *cr
162
163
164
165
166
167
168
169
170
)
{
	slap_overinst	*on = (slap_overinst *)be->bd_info;
	seqmod_info	*sm = (seqmod_info *)on->on_bi.bi_private;

	if ( sm ) {
		ldap_pvt_thread_mutex_destroy( &sm->sm_mutex );

		ch_free( sm );
171
		on->on_bi.bi_private = NULL;
172
173
174
175
176
177
178
179
180
181
182
183
184
	}

	return 0;
}

/* This overlay is set up for dynamic loading via moduleload. For static
 * configuration, you'll need to arrange for the slap_overinst to be
 * initialized and registered by some other function inside slapd.
 */

static slap_overinst 		seqmod;

int
185
seqmod_initialize()
186
187
{
	seqmod.on_bi.bi_type = "seqmod";
188
	seqmod.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	seqmod.on_bi.bi_db_open = seqmod_db_open;
	seqmod.on_bi.bi_db_close = seqmod_db_close;

	seqmod.on_bi.bi_op_modify = seqmod_op_mod;
	seqmod.on_bi.bi_op_modrdn = seqmod_op_mod;
	seqmod.on_bi.bi_extended = seqmod_op_extended;

	return overlay_register( &seqmod );
}

#if SLAPD_OVER_SEQMOD == SLAPD_MOD_DYNAMIC
int
init_module( int argc, char *argv[] )
{
203
	return seqmod_initialize();
204
205
206
207
}
#endif /* SLAPD_OVER_SEQMOD == SLAPD_MOD_DYNAMIC */

#endif /* defined(SLAPD_OVER_SEQMOD) */