1 /* txn.c - LDAP Transactions */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2014 The OpenLDAP Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
21 #include <ac/socket.h>
22 #include <ac/string.h>
23 #include <ac/unistd.h>
31 const struct berval slap_EXOP_TXN_START = BER_BVC(LDAP_EXOP_X_TXN_START);
32 const struct berval slap_EXOP_TXN_END = BER_BVC(LDAP_EXOP_X_TXN_END);
35 Operation *op, SlapReply *rs )
40 Statslog( LDAP_DEBUG_STATS, "%s TXN START\n",
41 op->o_log_prefix, 0, 0, 0, 0 );
43 if( op->ore_reqdata != NULL ) {
44 rs->sr_text = "no request data expected";
45 return LDAP_PROTOCOL_ERROR;
48 op->o_bd = op->o_conn->c_authz_backend;
49 if( backend_check_restrictions( op, rs,
50 (struct berval *)&slap_EXOP_TXN_START ) != LDAP_SUCCESS )
55 /* acquire connection lock */
56 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
58 if( op->o_conn->c_txn != CONN_TXN_INACTIVE ) {
59 rs->sr_text = "Too many transactions";
64 assert( op->o_conn->c_txn_backend == NULL );
65 op->o_conn->c_txn = CONN_TXN_SPECIFY;
67 bv = (struct berval *) ch_malloc( sizeof (struct berval) );
75 /* release connection lock */
76 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
81 Operation *op, SlapReply *rs, LDAPControl *ctrl )
83 if ( !ctrl->ldctl_iscritical ) {
84 rs->sr_text = "txnSpec control must be marked critical";
85 return LDAP_PROTOCOL_ERROR;
88 rs->sr_text = "txnSpec control provided multiple times";
89 return LDAP_PROTOCOL_ERROR;
92 if ( ctrl->ldctl_value.bv_val == NULL ) {
93 rs->sr_text = "no transaction identifier provided";
94 return LDAP_PROTOCOL_ERROR;
96 if ( ctrl->ldctl_value.bv_len != 0 ) {
97 rs->sr_text = "invalid transaction identifier";
98 return LDAP_X_TXN_ID_INVALID;
101 if ( op->o_preread ) { /* temporary limitation */
102 rs->sr_text = "cannot perform pre-read in transaction";
103 return LDAP_UNWILLING_TO_PERFORM;
105 if ( op->o_postread ) { /* temporary limitation */
106 rs->sr_text = "cannot perform post-read in transaction";
107 return LDAP_UNWILLING_TO_PERFORM;
110 op->o_txnSpec = SLAP_CONTROL_CRITICAL;
114 static int txn_result( Operation *op, SlapReply *rs )
120 Operation *op, SlapReply *rs )
123 BerElementBuffer berbuf;
124 BerElement *ber = (BerElement *)&berbuf;
130 Connection *c = op->o_conn;
132 Statslog( LDAP_DEBUG_STATS, "%s TXN END\n",
133 op->o_log_prefix, 0, 0, 0, 0 );
135 if( op->ore_reqdata == NULL ) {
136 rs->sr_text = "request data expected";
137 return LDAP_PROTOCOL_ERROR;
139 if( op->ore_reqdata->bv_len == 0 ) {
140 rs->sr_text = "empty request data";
141 return LDAP_PROTOCOL_ERROR;
144 op->o_bd = c->c_authz_backend;
145 if( backend_check_restrictions( op, rs,
146 (struct berval *)&slap_EXOP_TXN_END ) != LDAP_SUCCESS )
151 ber_init2( ber, op->ore_reqdata, 0 );
153 tag = ber_scanf( ber, "{" /*}*/ );
154 if( tag == LBER_ERROR ) {
155 rs->sr_text = "request data decoding error";
156 return LDAP_PROTOCOL_ERROR;
159 tag = ber_peek_tag( ber, &len );
160 if( tag == LBER_BOOLEAN ) {
161 tag = ber_scanf( ber, "b", &commit );
162 if( tag == LBER_ERROR ) {
163 rs->sr_text = "request data decoding error";
164 return LDAP_PROTOCOL_ERROR;
168 tag = ber_scanf( ber, /*{*/ "m}", &txnid );
169 if( tag == LBER_ERROR ) {
170 rs->sr_text = "request data decoding error";
171 return LDAP_PROTOCOL_ERROR;
175 rs->sr_text = "invalid transaction identifier";
176 return LDAP_X_TXN_ID_INVALID;
179 /* acquire connection lock */
180 ldap_pvt_thread_mutex_lock( &c->c_mutex );
182 if( c->c_txn != CONN_TXN_SPECIFY ) {
183 rs->sr_text = "invalid transaction identifier";
184 rc = LDAP_X_TXN_ID_INVALID;
187 c->c_txn = CONN_TXN_SETTLE;
190 slap_callback cb = {0};
192 if ( op->o_abandon ) {
196 if( LDAP_STAILQ_EMPTY(&c->c_txn_ops) ) {
197 /* no updates to commit */
198 rs->sr_text = "no updates to commit";
199 rc = LDAP_OPERATIONS_ERROR;
203 cb.sc_response = txn_result;
204 LDAP_STAILQ_FOREACH( o, &c->c_txn_ops, o_next ) {
205 o->o_bd = c->c_txn_backend;
208 rc = o->o_bd->bd_info->bi_op_txn(o, SLAP_TXN_BEGIN, &txn );
210 rs->sr_text = "couldn't start DB transaction";
215 LDAP_SLIST_INSERT_HEAD( &o->o_extra, txn, oe_next );
217 cb.sc_next = o->o_callback;
220 SlapReply rs = {REP_RESULT};
221 int opidx = slap_req2op( o->o_tag );
222 assert( opidx != SLAP_OP_LAST );
223 o->o_threadctx = op->o_threadctx;
224 o->o_tid = op->o_tid;
225 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
226 rc = (&o->o_bd->bd_info->bi_op_bind)[opidx]( o, &rs );
227 ldap_pvt_thread_mutex_lock( &c->c_mutex );
230 struct berval *bv = NULL;
231 BerElementBuffer berbuf;
232 BerElement *ber = (BerElement *)&berbuf;
234 ber_init_w_nullc( ber, LBER_USE_DER );
235 ber_printf( ber, "{i}", o->o_msgid );
236 ber_flatten( ber, &bv );
239 o->o_bd->bd_info->bi_op_txn(o, SLAP_TXN_ABORT, &txn );
244 rc = o->o_bd->bd_info->bi_op_txn(o, SLAP_TXN_COMMIT, &txn );
246 rs->sr_text = "transaction commit failed";
250 rs->sr_text = "transaction aborted";
255 /* drain txn ops list */
256 while (( o = LDAP_STAILQ_FIRST( &c->c_txn_ops )) != NULL ) {
257 LDAP_STAILQ_REMOVE_HEAD( &c->c_txn_ops, o_next );
258 LDAP_STAILQ_NEXT( o, o_next ) = NULL;
259 slap_op_free( o, NULL );
263 assert( LDAP_STAILQ_EMPTY(&c->c_txn_ops) );
264 assert( c->c_txn == CONN_TXN_SETTLE );
265 c->c_txn = CONN_TXN_INACTIVE;
266 c->c_txn_backend = NULL;
269 /* release connection lock */
270 ldap_pvt_thread_mutex_unlock( &c->c_mutex );
275 int txn_preop( Operation *op, SlapReply *rs )
279 /* acquire connection lock */
280 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
281 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
282 rs->sr_text = "invalid transaction identifier";
283 rs->sr_err = LDAP_X_TXN_ID_INVALID;
285 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
290 if( op->o_conn->c_txn_backend == NULL ) {
291 op->o_conn->c_txn_backend = op->o_bd;
293 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
294 rs->sr_text = "transaction cannot span multiple database contexts";
295 rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
299 /* insert operation into transaction */
300 LDAP_STAILQ_REMOVE( &op->o_conn->c_ops, op, Operation, o_next );
301 LDAP_STAILQ_INSERT_TAIL( &op->o_conn->c_txn_ops, op, o_next );
304 /* release connection lock */
305 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
308 send_ldap_result( op, rs );
310 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
313 return LDAP_SUCCESS; /* proceed with operation */
316 #endif /* LDAP_X_TXN */