1 /* txn.c - TP support functions of the bdb2 backend */
7 bdb2i_txn_head_init( BDB2_TXN_HEAD *head )
10 BDB2_TXN_FILES **fileNodeH;
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++ ) {
17 char fileName[MAXPATHLEN];
19 *fileNodeH = (BDB2_TXN_FILES *) ch_calloc( 1, sizeof( BDB2_TXN_FILES ));
20 if ( *fileNodeH == NULL ) {
22 Debug( LDAP_DEBUG_ANY, "bdb2i_txn_head_init(): out of memory!\n",
28 sprintf( fileName, "%s%s", bdb2i_fixed_filenames[dbFile], BDB2_SUFFIX );
29 (*fileNodeH)->dbc_name = ch_strdup( fileName );
31 fileNodeH = &(*fileNodeH)->next;
35 /* set defaults for checkpointing */
36 head->txn_log = BDB2_TXN_CHKP_MAX_LOG;
37 head->txn_time = BDB2_TXN_CHKP_MAX_TIME;
39 /* initialize the txn_dirty_mutex */
40 ldap_pvt_thread_mutex_init( &txn_dirty_mutex );
47 bdb2i_init_db_file_cache( struct ldbminfo *li, BDB2_TXN_FILES *fileinfo )
52 fileinfo->dbc_refcnt = 1;
54 sprintf( buf, "%s%s%s", li->li_directory, DEFAULT_DIRSEP,
56 if ( stat( buf, &st ) == 0 ) {
57 fileinfo->dbc_blksize = st.st_blksize;
59 fileinfo->dbc_blksize = DEFAULT_BLOCKSIZE;
62 fileinfo->dbc_maxids = ( fileinfo->dbc_blksize / sizeof( ID )) -
64 fileinfo->dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
65 fileinfo->dbc_maxids ) + 1;
70 /* create a DB file cache entry for a specified index attribute
71 (if not already done); the function is called during config
72 file read for all index'ed attributes; if "default" index with
73 a non-none selection is given, this is remembered for run-time
74 extension of the list of index files; the function is also
75 called before add or modify operations to check for putative
76 new "default" index files; at that time, files are also opened
79 bdb2i_txn_attr_config(
84 BDB2_TXN_HEAD *head = &li->li_txn_head;
86 /* the "attribute" 'default' is special */
87 if ( strcasecmp( attr, "default" )) {
89 /* create a new index file node, if the index is not known already */
90 BDB2_TXN_FILES **fileNodeH;
91 char fileName[MAXPATHLEN];
93 sprintf( fileName, "%s%s", attr, BDB2_SUFFIX );
95 /* search for the end of the list or a node describing
96 the current attribute */
97 for ( fileNodeH = &head->dbFiles;
98 ( *fileNodeH && strcasecmp( (*fileNodeH)->dbc_name, fileName ));
99 fileNodeH = &(*fileNodeH)->next ) {
103 /* unless we have that attribute already... */
104 if ( *fileNodeH == NULL ) {
107 Debug( LDAP_DEBUG_TRACE,
108 "bdb2i_txn_attr_config(): adding node for \"%s\"\n",
111 /* if we're out of memory, we have to see, how to exit... */
112 if ( ( *fileNodeH = p = (BDB2_TXN_FILES *)
113 ch_calloc( 1, sizeof( BDB2_TXN_FILES )) ) == NULL ) {
115 Debug( LDAP_DEBUG_ANY,
116 "bdb2i_txn_attr_config(): out of memory -- FATAL.\n",
119 /* during configuration (no files are opened)
120 we can just exit, otherwise we kill ourself and
121 hope to shutdown cleanly... */
123 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
129 p->dbc_name = ch_strdup( fileName );
131 /* if requested for, we have to open the DB file */
132 /* BUT NOT "objectclass", 'cause that's a default index ! */
133 if ( open && strcasecmp( fileName, "objectclass" )) {
135 /* re-use filename to get the complete path */
136 sprintf( fileName, "%s%s%s",
137 li->li_directory, DEFAULT_DIRSEP, p->dbc_name );
139 /* since we have an mpool, we should not define a cache size */
140 p->dbc_db = bdb2i_db_open( fileName, DB_TYPE,
141 LDBM_WRCREAT, li->li_mode, 0 );
143 /* if the files could not be opened, something is wrong;
145 if ( p->dbc_db == NULL ) {
147 Debug( LDAP_DEBUG_ANY,
148 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
150 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
154 bdb2i_init_db_file_cache( li, p );
156 Debug( LDAP_DEBUG_TRACE,
157 "bdb2i_txn_attr_config(): NEW INDEX FILE \"%s\"\n",
163 } else { /* it is "attribute" 'default' */
165 head->withDefIDX = BDB2_WITH_DEF_IDX;
171 /* open the NEXTID file for read/write; if it does not exist,
172 create it (access to the file must be preceeded by a rewind)
175 bdb2i_open_nextid( BackendDB *be )
177 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
178 BDB2_TXN_HEAD *head = &li->li_txn_head;
181 char fileName[MAXPATHLEN];
183 sprintf( fileName, "%s%s%s",
184 li->li_directory, DEFAULT_DIRSEP, NEXTID_NAME );
186 /* try to open the file for read and write */
187 memset( &dbinfo, 0, sizeof( dbinfo ));
188 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
189 dbinfo.db_malloc = ldbm_malloc;
191 (void) db_open( fileName, DB_RECNO, DB_CREATE | DB_THREAD,
192 li->li_mode, &bdb2i_dbEnv, &dbinfo, &db );
196 Debug( LDAP_DEBUG_ANY,
197 "bdb2i_open_nextid: could not open \"%s\"\n",
203 /* the file is open for read/write */
204 head->nextidFile = db;
210 /* open all DB during startup of the backend (necessary due to TP)
211 additional files may be opened during slapd life-time due to
212 default indexes (must be configured in slapd.conf;
213 see bdb2i_txn_attr_config)
214 also, set the counter and timer for TP checkpointing
217 bdb2i_txn_open_files( BackendDB *be )
219 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
220 BDB2_TXN_HEAD *head = &li->li_txn_head;
221 BDB2_TXN_FILES *dbFile;
224 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
225 char fileName[MAXPATHLEN];
227 sprintf( fileName, "%s%s%s",
228 li->li_directory, DEFAULT_DIRSEP, dbFile->dbc_name );
230 /* since we have an mpool, we should not define a cache size */
231 dbFile->dbc_db = bdb2i_db_open( fileName, DB_TYPE,
232 LDBM_WRCREAT, li->li_mode, 0 );
234 /* if the files could not be opened, something is wrong; complain */
235 if ( dbFile->dbc_db == NULL ) {
237 Debug( LDAP_DEBUG_ANY,
238 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
239 dbFile->dbc_name, 0, 0 );
244 /* initialize the file info */
245 bdb2i_init_db_file_cache( li, dbFile );
247 Debug( LDAP_DEBUG_TRACE, "bdb2i_txn_open_files(): OPEN INDEX \"%s\"\n",
248 dbFile->dbc_name, 0, 0 );
252 rc = bdb2i_open_nextid( be );
254 txn_max_pending_log = head->txn_log;
255 txn_max_pending_time = head->txn_time;
261 /* close all DB files during shutdown of the backend */
263 bdb2i_txn_close_files( BackendDB *be )
265 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
266 BDB2_TXN_HEAD *head = &li->li_txn_head;
267 BDB2_TXN_FILES *dbFile;
269 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
271 ldbm_close( dbFile->dbc_db );
275 ldbm_close( head->nextidFile );
280 /* get the db_cache structure associated with a specified
281 DB file (replaces the on-the-fly opening of files in cache_open()
284 bdb2i_get_db_file_cache( struct ldbminfo *li, char *name )
286 BDB2_TXN_HEAD *head = &li->li_txn_head;
287 BDB2_TXN_FILES *dbFile;
290 Debug( LDAP_DEBUG_TRACE, "bdb2i_get_db_file_cache(): looking for file %s\n",
293 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
296 if ( !strcasecmp( dbFile->dbc_name, name )) return( dbFile );
300 Debug( LDAP_DEBUG_ANY,
301 "bdb2i_get_db_file_cache(): UPS, could't find \"%s\" \n", name, 0, 0 );
303 /* ups, we couldn't find the file */
309 /* check for new attribute indexes, that might have been created
310 during former runs of slapd */
311 /* this is called during startup of the slapd server */
313 bdb2i_check_additional_attr_index( struct ldbminfo *li )
318 if ( ( datadir = opendir( li->li_directory ) ) == NULL ) {
320 Debug( LDAP_DEBUG_ANY,
321 "bdb2i_check_additional_attr_index(): ERROR while opening datadir: %s\n",
322 strerror( errno ), 0, 0 );
327 for ( file = readdir( datadir ); file; file = readdir( datadir )) {
328 char filename[MAXPATHLEN];
331 strcpy( filename, file->d_name );
332 namelen = strlen( filename );
334 if ( namelen > strlen( BDB2_SUFFIX )) {
336 if ( !strcasecmp( filename + namelen - strlen( BDB2_SUFFIX ),
339 *(filename + namelen - strlen( BDB2_SUFFIX )) = '\0';
340 bdb2i_txn_attr_config( li, filename, 0 );
342 Debug( LDAP_DEBUG_TRACE, "INDEX FILE: %s\n", filename, 0, 0 );
356 /* check for the addition of new attribute indexes during add */
357 /* this is called after startup of the slapd server */
358 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
359 FROM ADDING ATTRIBUTES LATER ON */
361 bdb2i_check_default_attr_index_add( struct ldbminfo *li, Entry *e )
363 BDB2_TXN_HEAD *head = &li->li_txn_head;
365 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
368 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
369 if ( strcasecmp( ap->a_type, "objectclass" ))
370 bdb2i_txn_attr_config( li, ap->a_type, 1 );
376 /* check for the addition of new attribute indexes during modify */
377 /* this is called after startup of the slapd server */
378 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
379 FROM ADDING ATTRIBUTES LATER ON */
381 bdb2i_check_default_attr_index_mod( struct ldbminfo *li, LDAPModList *modlist )
383 BDB2_TXN_HEAD *head = &li->li_txn_head;
385 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
387 char *default_attrs[] = { "modifytimestamp", "modifiersname", NULL };
390 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
391 LDAPMod *mod = &ml->ml_mod;
393 if (( mod->mod_op & ~LDAP_MOD_BVALUES ) == LDAP_MOD_ADD )
394 if ( strcasecmp( mod->mod_type, "objectclass" ))
395 bdb2i_txn_attr_config( li, mod->mod_type, 1 );
398 /* these attributes are default when modifying */
399 for ( attr = 0; default_attrs[attr]; attr++ ) {
400 bdb2i_txn_attr_config( li, default_attrs[attr], 1 );
406 /* get the next ID from the NEXTID file */
408 bdb2i_get_nextid( BackendDB *be )
410 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
411 BDB2_TXN_HEAD *head = &li->li_txn_head;
415 db_recno_t rec = NEXTID_RECNO;
417 ldbm_datum_init( key );
418 ldbm_datum_init( data );
421 key.size = sizeof( rec );
423 data = bdb2i_db_fetch( head->nextidFile, key );
424 if ( data.data == NULL ) {
425 Debug( LDAP_DEBUG_ANY,
426 "next_id_read: could not get nextid from \"%s\"\n",
431 id = atol( data.data );
432 ldbm_datum_free( head->nextidFile, data );
435 Debug( LDAP_DEBUG_ANY,
436 "next_id_read %ld: return non-positive integer\n",
446 bdb2i_put_nextid( BackendDB *be, ID id )
448 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
449 BDB2_TXN_HEAD *head = &li->li_txn_head;
453 db_recno_t rec = NEXTID_RECNO;
456 sprintf( buf, "%ld\n", id );
458 ldbm_datum_init( key );
459 ldbm_datum_init( data );
462 key.size = sizeof( rec );
465 data.size = sizeof( buf );
467 flags = LDBM_REPLACE;
468 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
470 if (( rc = bdb2i_db_store( head->nextidFile, key, data, flags )) != 0 ) {
471 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): store failed (%d)\n",
480 /* BDB2 backend-private functions of libldbm */
492 memset( &dbinfo, 0, sizeof( dbinfo ));
493 if ( bdb2i_dbEnv.mp_info == NULL )
494 dbinfo.db_cachesize = dbcachesize;
495 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
496 dbinfo.db_malloc = ldbm_malloc;
498 (void) db_open( name, type, rw, mode, &bdb2i_dbEnv, &dbinfo, &ret );
505 bdb2i_db_store( LDBM ldbm, Datum key, Datum data, int flags )
509 rc = (*ldbm->put)( ldbm, txnid, &key, &data, flags & ~LDBM_SYNC );
512 if ( txnid != NULL ) {
514 /* if the store was OK, set the dirty flag,
515 otherwise set the abort flag */
522 Debug( LDAP_DEBUG_ANY,
523 "bdb2i_db_store: transaction failed: aborted.\n",
535 bdb2i_db_delete( LDBM ldbm, Datum key )
539 rc = (*ldbm->del)( ldbm, txnid, &key, 0 );
542 if ( txnid != NULL ) {
544 /* if the delete was OK, set the dirty flag,
545 otherwise set the abort flag */
552 Debug( LDAP_DEBUG_ANY,
553 "bdb2i_db_delete: transaction failed: aborted.\n",
565 bdb2i_db_fetch( LDBM ldbm, Datum key )
570 ldbm_datum_init( data );
571 data.flags = DB_DBT_MALLOC;
573 if ( (rc = (*ldbm->get)( ldbm, txnid, &key, &data, 0 )) != 0 ) {
574 if (( txnid != NULL ) && ( rc != DB_NOTFOUND )) {
576 Debug( LDAP_DEBUG_ANY,
577 "bdb2i_db_fetch: transaction failed: aborted.\n",
582 if ( data.dptr ) free( data.dptr );
592 bdb2i_db_firstkey( LDBM ldbm, DBC **dbch )
598 ldbm_datum_init( key );
599 ldbm_datum_init( data );
601 key.flags = data.flags = DB_DBT_MALLOC;
603 #if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
604 DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6
606 if ( (*ldbm->cursor)( ldbm, txnid, &dbci ))
610 if ( (*ldbm->cursor)( ldbm, txnid, &dbci, 0 ))
614 if ( txnid != NULL ) {
616 Debug( LDAP_DEBUG_ANY,
617 "bdb2i_db_firstkey: transaction failed: aborted.\n",
626 if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
627 ldbm_datum_free( ldbm, data );
629 if ( txnid != NULL ) {
631 Debug( LDAP_DEBUG_ANY,
632 "bdb2i_db_firstkey: transaction failed: aborted.\n",
637 ldbm_datum_free( ldbm, key );
649 bdb2i_db_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
654 ldbm_datum_init( data );
655 key.flags = data.flags = DB_DBT_MALLOC;
657 if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
658 ldbm_datum_free( ldbm, data );
660 if ( txnid != NULL ) {
662 Debug( LDAP_DEBUG_ANY,
663 "bdb2i_db_nextkey: transaction failed: aborted.\n",
677 /* Transaction control of write access */
678 /* Since these functions are only used by one writer at a time,
679 we do not have any concurrency (locking) problem */
681 /* initialize a new transaction */
683 bdb2i_start_transction( DB_TXNMGR *txmgr )
690 if (( rc = txn_begin( txmgr, NULL, &txnid )) != 0 ) {
691 Debug( LDAP_DEBUG_ANY, "bdb2i_start_transction failed: %d: errno=%s\n",
692 rc, strerror( errno ), 0 );
695 (void) txn_abort( txnid );
699 Debug( LDAP_DEBUG_TRACE,
700 "bdb2i_start_transaction: transaction started.\n",
707 /* finish the transaction */
709 bdb2i_finish_transaction()
713 /* if transaction was NOT selected, just return */
714 if ( txnid == NULL ) return( 0 );
716 /* if nothing was wrong so far, we can try to commit the transaction */
717 /* complain, if the commit fails */
718 if (( txn_do_abort == 0 ) && ( txn_commit( txnid )) != 0 ) {
719 Debug( LDAP_DEBUG_ANY,
720 "bdb2i_finish_transaction: transaction commit failed: aborted.\n",
725 /* if anything went wrong, we have to abort the transaction */
726 if ( txn_do_abort ) {
727 Debug( LDAP_DEBUG_ANY,
728 "bdb2i_finish_transaction: transaction aborted.\n",
730 (void) txn_abort( txnid );
733 Debug( LDAP_DEBUG_TRACE,
734 "bdb2i_finish_transaction: transaction commited.\n",
738 /* XXX do NOT free the txnid memory !!! */
747 either forced (during shutdown) or when logsize or time are exceeded
748 (is called by reader and writer, so protect txn_dirty)
751 bdb2i_set_txn_checkpoint( DB_TXNMGR *txmgr, int forced )
755 /* set dirty mutex */
756 ldap_pvt_thread_mutex_lock( &txn_dirty_mutex );
764 logsize = forced ? (u_int32_t) 0 : txn_max_pending_log;
765 mins = forced ? (u_int32_t) 0 : txn_max_pending_time;
768 now = slap_get_time();
770 rc = txn_checkpoint( txmgr, logsize, mins );
772 /* if checkpointing was successful, reset txn_dirty */
774 DB_TXN_STAT *statp = NULL;
776 /* check whether the checkpoint was actually written;
777 if so, unset the txn_dirty flag */
778 if (( rc = txn_stat( txmgr, &statp, ldbm_malloc )) == 0 ) {
780 if ( statp && ( statp->st_time_ckp >= now )) {
782 Debug( LDAP_DEBUG_TRACE,
783 "bdb2i_set_txn_checkpoint succeded.\n",
789 if ( statp ) free( statp );
792 Debug( LDAP_DEBUG_ANY,
793 "bdb2i_set_txn_checkpoint: txn_stat failed: %d\n",
797 Debug( LDAP_DEBUG_ANY, "bdb2i_set_txn_checkpoint failed: %d\n",
802 /* release dirty mutex */
803 ldap_pvt_thread_mutex_unlock( &txn_dirty_mutex );