]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/ctxcsn.c
memory mgmt cleanup
[openldap] / servers / slapd / back-bdb / ctxcsn.c
1 /* $OpenLDAP$ */
2 /*
3  * back-bdb Context CSN Management Routines
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/time.h>
29
30 #include "back-bdb.h"
31 #include "external.h"
32
33 int
34 bdb_csn_commit(
35         Operation *op,
36         SlapReply *rs,
37         DB_TXN *tid,
38         EntryInfo *ei,
39         EntryInfo **suffix_ei,
40         Entry **ctxcsn_e,
41         int *ctxcsn_added,
42         u_int32_t locker
43 )
44 {
45         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
46         struct berval   ctxcsn_rdn = { 0, NULL };
47         struct berval   ctxcsn_ndn = { 0, NULL };
48         EntryInfo               *ctxcsn_ei = NULL;
49         DB_LOCK                 ctxcsn_lock;
50         struct berval   *max_committed_csn = NULL;
51         DB_LOCK                 suffix_lock;
52         int                             rc, ret;
53         ID                              ctxcsn_id;
54         Entry                   *e;
55
56         if ( ei ) {
57                 e = ei->bei_e;
58         }
59
60         ber_str2bv( "cn=ldapsync", strlen("cn=ldapsync"), 0, &ctxcsn_rdn );
61         build_new_dn( &ctxcsn_ndn, &op->o_bd->be_nsuffix[0], &ctxcsn_rdn );
62
63         rc = bdb_dn2entry( op, tid, &ctxcsn_ndn, &ctxcsn_ei,
64                                                            0, locker, &ctxcsn_lock );
65
66         if ( ctxcsn_ei ) {
67                 *ctxcsn_e = ctxcsn_ei->bei_e;
68                 bdb_cache_entry_db_relock( bdb->bi_dbenv, locker, ctxcsn_ei, 1, 0, &ctxcsn_lock );
69         }
70
71         max_committed_csn = slap_get_commit_csn( op );
72
73         if ( max_committed_csn == NULL )
74                 return BDB_CSN_COMMIT;
75
76         *ctxcsn_added = 0;
77
78         switch( rc ) {
79         case 0:
80                 if ( !*ctxcsn_e ) {
81                         rs->sr_err = LDAP_OTHER;
82                         rs->sr_text = "context csn not present";
83                         ber_bvfree( max_committed_csn );
84                         return BDB_CSN_ABORT;
85                 } else {
86                         attr_delete( &(*ctxcsn_e)->e_attrs, slap_schema.si_ad_contextCSN );
87                         attr_merge_normalize_one( *ctxcsn_e, slap_schema.si_ad_contextCSN,
88                                                         max_committed_csn, NULL );
89                         ret = bdb_id2entry_update( op->o_bd, tid, *ctxcsn_e );
90                         switch ( ret ) {
91                         case 0 :
92                                 break;
93                         case DB_LOCK_DEADLOCK :
94                         case DB_LOCK_NOTGRANTED :
95                                 goto rewind;
96                         default :
97                                 rs->sr_err = ret;
98                                 rs->sr_text = "context csn update failed";
99                                 return BDB_CSN_ABORT;
100                         }
101                         ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
102                         switch ( ret ) {
103                         case 0 :
104                                 break;
105                         case DB_LOCK_DEADLOCK :
106                         case DB_LOCK_NOTGRANTED :
107                                 goto rewind;
108                         default :
109                                 rs->sr_err = LDAP_OTHER;
110                                 rs->sr_text = "context csn indexing failed";
111                                 return BDB_CSN_ABORT;
112                         }
113                 }
114                 break;
115         case DB_NOTFOUND:
116                 if ( op->o_tag == LDAP_REQ_ADD && !be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname )) {
117                         rc = bdb_dn2entry( op, tid, &op->o_bd->be_nsuffix[0], suffix_ei,
118                                                                         0, locker, &suffix_lock );
119                 } else if ( op->o_tag != LDAP_REQ_ADD && !be_issuffix( op->o_bd, &e->e_nname )) {
120                         rc = bdb_dn2entry( op, tid, &op->o_bd->be_nsuffix[0], suffix_ei,
121                                                                         0, locker, &suffix_lock );
122                 } else {
123                         *suffix_ei = ei;
124                 }
125
126                 /* This serializes add. But this case is very rare : only once. */
127                 rs->sr_err = bdb_next_id( op->o_bd, tid, &ctxcsn_id );
128                 if ( rs->sr_err != 0 ) {
129 #ifdef NEW_LOGGING
130                         LDAP_LOG ( OPERATION, ERR,
131                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
132 #else
133                         Debug( LDAP_DEBUG_TRACE,
134                                 "bdb_add: next_id failed (%d)\n", rs->sr_err, 0, 0 );
135 #endif
136                         rs->sr_err = LDAP_OTHER;
137                         rs->sr_text = "internal error";
138                         return BDB_CSN_ABORT;
139                 }
140
141                 *ctxcsn_e = slap_create_context_csn_entry( op->o_bd, max_committed_csn );
142                 ber_bvfree( max_committed_csn );
143                 (*ctxcsn_e)->e_id = ctxcsn_id;
144                 *ctxcsn_added = 1;
145                 ret = bdb_dn2id_add( op, tid, *suffix_ei, *ctxcsn_e );
146                 switch ( ret ) {
147                 case 0 :
148                         break;
149                 case DB_LOCK_DEADLOCK :
150                 case DB_LOCK_NOTGRANTED :
151                         goto rewind;
152                 case DB_KEYEXIST :
153                         rs->sr_err = LDAP_OTHER;
154                         rs->sr_text = "context csn exists before contex prefix does";
155                         return BDB_CSN_ABORT;
156                 default :
157                         rs->sr_err = LDAP_OTHER;
158                         rs->sr_text = "context csn store failed";
159                         return BDB_CSN_ABORT;
160                 }
161                 ret = bdb_id2entry_add( op->o_bd, tid, *ctxcsn_e );
162                 switch ( ret ) {
163                 case 0 :
164                         break;
165                 case DB_LOCK_DEADLOCK :
166                 case DB_LOCK_NOTGRANTED :
167                         goto rewind;
168                 default :
169                         rs->sr_err = LDAP_OTHER;
170                         rs->sr_text = "context csn store failed";
171                         return BDB_CSN_ABORT;
172                 }
173                 ret = bdb_index_entry_add( op, tid, *ctxcsn_e );
174                 switch ( ret ) {
175                 case 0 :
176                         break;
177                 case DB_LOCK_DEADLOCK :
178                 case DB_LOCK_NOTGRANTED :
179                         goto rewind;
180                 default :
181                         rs->sr_err = LDAP_OTHER;
182                         rs->sr_text = "context csn indexing failed";
183                         return BDB_CSN_ABORT;
184                 }
185                 break;
186         case DB_LOCK_DEADLOCK:
187         case DB_LOCK_NOTGRANTED:
188                 goto rewind;
189         case LDAP_BUSY:
190                 rs->sr_err = rc;
191                 rs->sr_text = "ldap server busy";
192                 return BDB_CSN_ABORT;
193         default:
194                 rs->sr_err = LDAP_OTHER;
195                 rs->sr_text = "internal error";
196                 return BDB_CSN_ABORT;
197         }
198
199         return BDB_CSN_COMMIT;
200
201 rewind :
202         slap_rewind_commit_csn( op );
203         return BDB_CSN_RETRY;
204 }