]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb2/txn.c
Elimination of un-used code in bdb2i_cache_open and friends.
[openldap] / servers / slapd / back-bdb2 / txn.c
1 /* txn.c - TP support functions of the bdb2 backend */
2
3 #include "txn.h"
4
5
6 int
7 bdb2i_txn_head_init( BDB2_TXN_HEAD *head )
8 {
9         int             dbFile;
10         BDB2_TXN_FILES  **fileNodeH;
11
12         /*  for each fixed DB file allocate a file descriptor node and
13         initialize the file's name  */
14         fileNodeH = &head->dbFiles;
15         for ( dbFile = BDB2_DB_DN_FILE; dbFile <= BDB2_DB_OC_IDX_FILE; dbFile++ ) {
16
17                 char fileName[MAXPATHLEN];
18
19                 *fileNodeH = (BDB2_TXN_FILES *) ch_calloc( 1, sizeof( BDB2_TXN_FILES ));
20                 if ( *fileNodeH == NULL ) {
21
22                         Debug( LDAP_DEBUG_ANY, "bdb2i_txn_head_init(): out of memory!\n",
23                                         0, 0, 0 );
24                         return( 1 );
25
26                 }
27
28                 sprintf( fileName, "%s%s", bdb2i_fixed_filenames[dbFile], BDB2_SUFFIX );
29                 (*fileNodeH)->dbc_name = strdup( fileName );
30
31                 fileNodeH = &(*fileNodeH)->next;
32
33         }
34
35         return 0;
36 }
37
38
39 static void
40 bdb2i_init_db_file_cache( struct ldbminfo *li, BDB2_TXN_FILES *fileinfo )
41 {
42         time_t      curtime;
43         struct stat st;
44         char        buf[MAXPATHLEN];
45
46         ldap_pvt_thread_mutex_lock( &currenttime_mutex );
47         curtime = currenttime;
48         ldap_pvt_thread_mutex_unlock( &currenttime_mutex );
49
50         fileinfo->dbc_refcnt = 1;
51         fileinfo->dbc_lastref = curtime;
52
53         sprintf( buf, "%s%s%s", li->li_directory, DEFAULT_DIRSEP,
54                                         fileinfo->dbc_name );
55         if ( stat( buf, &st ) == 0 ) {
56                 fileinfo->dbc_blksize = st.st_blksize;
57         } else {
58                 fileinfo->dbc_blksize = DEFAULT_BLOCKSIZE;
59         }
60
61         fileinfo->dbc_maxids = ( fileinfo->dbc_blksize / sizeof( ID )) -
62                         ID_BLOCK_IDS_OFFSET;
63         fileinfo->dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
64                 fileinfo->dbc_maxids ) + 1;
65
66 }
67
68
69 void
70 bdb2i_txn_attr_config(
71         struct ldbminfo  *li,
72         char             *attr,
73         int              open )
74 {
75         BDB2_TXN_HEAD  *head = &li->li_txn_head;
76
77         /*  the "attribute" 'default' is special  */
78         if ( strcasecmp( attr, "default" )) {
79
80                 /*  create a new index file node, if the index is not known  already  */
81                 BDB2_TXN_FILES  **fileNodeH;
82                 char            fileName[MAXPATHLEN];
83
84                 sprintf( fileName, "%s%s", attr,  BDB2_SUFFIX );
85
86                 /*  search for the end of the list or a node describing
87                         the current attribute  */
88                 for ( fileNodeH = &head->dbFiles;
89                                 ( *fileNodeH && strcasecmp( (*fileNodeH)->dbc_name, fileName ));
90                                 fileNodeH = &(*fileNodeH)->next ) {
91
92                 }
93
94                 /*  unless we have that attribute already...  */
95                 if ( *fileNodeH == NULL ) {
96                         BDB2_TXN_FILES *p;
97
98                         Debug( LDAP_DEBUG_TRACE,
99                                         "bdb2i_txn_attr_config(): adding node for \"%s\"\n",
100                                         fileName, 0, 0 );
101
102                         /*  if we're out of memory, we have to see, how to exit...  */
103                         if ( ( *fileNodeH = p = (BDB2_TXN_FILES *)
104                                         ch_calloc( 1, sizeof( BDB2_TXN_FILES )) ) == NULL ) {
105
106                                 Debug( LDAP_DEBUG_ANY,
107                                                 "bdb2i_txn_attr_config(): out of memory -- FATAL.\n",
108                                                 0, 0, 0 );
109
110                                 /*  during configuration (no files are opened)
111                                         we can just exit, otherwise we kill ourself and
112                                         hope to shutdown cleanly...  */
113                                 if ( open ) {
114                                         pthread_kill( pthread_self(), LDAP_SIGUSR1 );
115                                 } else {
116                                         exit( 1 );
117                                 }
118                         }
119
120                         p->dbc_name = strdup( fileName );
121
122                         /*  if requested for, we have to open the DB file  */
123                         /*  BUT NOT "objectclass", 'cause that's a default index !  */
124                         if ( open && strcasecmp( fileName, "objectclass" )) {
125
126                                 /*  re-use filename to get the complete path  */
127                                 sprintf( fileName, "%s%s%s",
128                                                         li->li_directory, DEFAULT_DIRSEP, p->dbc_name );
129
130                                 /*  since we have an mpool, we should not define a cache size */
131                                 p->dbc_db = ldbm_open( fileName, LDBM_WRCREAT, li->li_mode, 0 );
132
133                                 /*  if the files could not be opened, something is wrong;
134                                         complain  */
135                                 if ( p->dbc_db == NULL ) {
136
137                                         Debug( LDAP_DEBUG_ANY,
138                                 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
139                                                 p->dbc_name, 0, 0 );
140                                         pthread_kill( pthread_self(), LDAP_SIGUSR1 );
141
142                                 }
143
144                                 bdb2i_init_db_file_cache( li, p );
145
146                                 Debug( LDAP_DEBUG_TRACE,
147                                         "bdb2i_txn_attr_config(): NEW INDEX FILE \"%s\"\n",
148                                         p->dbc_name, 0, 0 );
149
150                         }
151                 }
152
153         } else {  /*  it is "attribute" 'default'  */
154
155                 head->withDefIDX = BDB2_WITH_DEF_IDX;
156
157         }
158 }
159
160
161 int
162 bdb2i_txn_open_files( struct ldbminfo *li )
163 {
164         BDB2_TXN_HEAD   *head = &li->li_txn_head;
165         BDB2_TXN_FILES  *dbFile;
166
167         for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
168                 char   fileName[MAXPATHLEN];
169
170                 sprintf( fileName, "%s%s%s",
171                                         li->li_directory, DEFAULT_DIRSEP, dbFile->dbc_name );
172
173                 /*  since we have an mpool, we should not define a cache size */
174                 dbFile->dbc_db = ldbm_open( fileName, LDBM_WRCREAT, li->li_mode, 0 );
175
176                 /*  if the files could not be opened, something is wrong; complain  */
177                 if ( dbFile->dbc_db == NULL ) {
178
179                         Debug( LDAP_DEBUG_ANY,
180                                 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
181                                 dbFile->dbc_name, 0, 0 );
182                         return( 1 );
183
184                 }
185
186                 /*  initialize the file info  */
187                 bdb2i_init_db_file_cache( li, dbFile );
188
189                 Debug( LDAP_DEBUG_TRACE, "bdb2i_txn_open_files(): OPEN INDEX \"%s\"\n",
190                                 dbFile->dbc_name, 0, 0 );
191
192         }
193
194         return 0;
195 }
196
197
198 void
199 bdb2i_txn_close_files( BackendDB *be )
200 {
201         struct ldbminfo  *li = (struct ldbminfo *) be->be_private;
202         BDB2_TXN_HEAD    *head = &li->li_txn_head;
203         BDB2_TXN_FILES   *dbFile;
204
205         for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
206
207                 ldbm_close( dbFile->dbc_db );
208
209         }
210 }
211
212
213 BDB2_TXN_FILES *
214 bdb2i_get_db_file_cache( struct ldbminfo *li, char *name )
215 {
216         BDB2_TXN_HEAD  *head = &li->li_txn_head;
217         BDB2_TXN_FILES *dbFile;
218         int            dbFileNum;
219
220         Debug( LDAP_DEBUG_TRACE, "bdb2i_get_db_file_cache(): looking for file %s\n",
221                         name, 0, 0 );
222
223         for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
224
225                 /*  we've got it  */
226                 if ( !strcasecmp( dbFile->dbc_name, name )) return( dbFile );
227
228         }
229
230         Debug( LDAP_DEBUG_ANY,
231                 "bdb2i_get_db_file_cache(): UPS, could't find \"%s\" \n", name, 0, 0 );
232
233         /*  ups, we couldn't find the file  */
234         return( NULL );
235
236 }
237
238
239 /*  check for new attribute indexes, that might have been created
240     during former runs of slapd  */
241 /*  this is called during startup of the slapd server  */
242 int
243 bdb2i_check_additional_attr_index( struct ldbminfo *li )
244 {
245         DIR            *datadir;
246         struct dirent  *file;
247
248         if ( ( datadir = opendir( li->li_directory ) ) == NULL ) {
249
250                 Debug( LDAP_DEBUG_ANY,
251         "bdb2i_check_additional_attr_index(): ERROR while opening datadir: %s\n",
252                                 strerror( errno ), 0, 0 );
253                 return( 1 );
254
255         }
256
257         for ( file = readdir( datadir ); file; file = readdir( datadir )) {
258                 char  filename[MAXPATHLEN];
259                 int   namelen;
260
261                 strcpy( filename, file->d_name );
262                 namelen = strlen( filename );
263
264                 if ( namelen > strlen( BDB2_SUFFIX )) {
265
266                         if ( !strcasecmp( filename + namelen - strlen( BDB2_SUFFIX ),
267                                                         BDB2_SUFFIX )) {
268
269                                 *(filename + namelen - strlen( BDB2_SUFFIX )) = '\0';
270                                 bdb2i_txn_attr_config( li, filename, 0 );
271
272                                 Debug( LDAP_DEBUG_TRACE, "INDEX FILE: %s\n", filename, 0, 0 );
273
274                         }
275
276                 }
277
278         }
279
280         closedir( datadir );
281
282         return 0;
283 }
284
285
286 /*  check for the addition of new attribute indexes during add  */
287 /*  this is called after startup of the slapd server  */
288 /*  DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
289         FROM ADDING ATTRIBUTES LATER ON  */
290 void
291 bdb2i_check_default_attr_index_add( struct ldbminfo *li, Entry *e )
292 {
293         BDB2_TXN_HEAD  *head = &li->li_txn_head;
294
295         if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
296                 Attribute   *ap;
297
298                 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
299                         if ( strcasecmp( ap->a_type, "objectclass" ))
300                                 bdb2i_txn_attr_config( li, ap->a_type, 1 );
301                 }
302         }
303 }
304
305
306 /*  check for the addition of new attribute indexes during modify  */
307 /*  this is called after startup of the slapd server  */
308 /*  DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
309         FROM ADDING ATTRIBUTES LATER ON  */
310 void
311 bdb2i_check_default_attr_index_mod( struct ldbminfo *li, LDAPModList *modlist )
312 {
313         BDB2_TXN_HEAD  *head = &li->li_txn_head;
314
315         if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
316                 LDAPModList *ml;
317                 char  *default_attrs[] = { "modifytimestamp", "modifiersname", NULL };
318                 int   attr;
319
320                 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
321                         LDAPMod *mod = &ml->ml_mod;
322
323                         if (( mod->mod_op & ~LDAP_MOD_BVALUES ) == LDAP_MOD_ADD )
324                                 if ( strcasecmp( mod->mod_type, "objectclass" ))
325                                         bdb2i_txn_attr_config( li, mod->mod_type, 1 );
326                 }
327
328                 /*  these attributes are default when modifying  */
329                 for ( attr = 0; default_attrs[attr]; attr++ ) {
330                         bdb2i_txn_attr_config( li, default_attrs[attr], 1 );
331                 }
332         }
333 }
334
335