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 if ( head->nextidFile )
276 ldbm_close( head->nextidFile );
281 /* get the db_cache structure associated with a specified
282 DB file (replaces the on-the-fly opening of files in cache_open()
285 bdb2i_get_db_file_cache( struct ldbminfo *li, char *name )
287 BDB2_TXN_HEAD *head = &li->li_txn_head;
288 BDB2_TXN_FILES *dbFile;
291 Debug( LDAP_DEBUG_TRACE, "bdb2i_get_db_file_cache(): looking for file %s\n",
294 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
297 if ( !strcasecmp( dbFile->dbc_name, name )) return( dbFile );
301 Debug( LDAP_DEBUG_ANY,
302 "bdb2i_get_db_file_cache(): UPS, could't find \"%s\" \n", name, 0, 0 );
304 /* ups, we couldn't find the file */
310 /* check for new attribute indexes, that might have been created
311 during former runs of slapd */
312 /* this is called during startup of the slapd server */
314 bdb2i_check_additional_attr_index( struct ldbminfo *li )
319 if ( ( datadir = opendir( li->li_directory ) ) == NULL ) {
322 Debug( LDAP_DEBUG_ANY,
323 "bdb2i_check_additional_attr_index(): ERROR while opening datadir: %s\n",
324 strerror( err ), 0, 0 );
329 for ( file = readdir( datadir ); file; file = readdir( datadir )) {
330 char filename[MAXPATHLEN];
333 strcpy( filename, file->d_name );
334 namelen = strlen( filename );
336 if ( namelen > strlen( BDB2_SUFFIX )) {
338 if ( !strcasecmp( filename + namelen - strlen( BDB2_SUFFIX ),
341 *(filename + namelen - strlen( BDB2_SUFFIX )) = '\0';
342 bdb2i_txn_attr_config( li, filename, 0 );
344 Debug( LDAP_DEBUG_TRACE, "INDEX FILE: %s\n", filename, 0, 0 );
358 /* check for the addition of new attribute indexes during add */
359 /* this is called after startup of the slapd server */
360 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
361 FROM ADDING ATTRIBUTES LATER ON */
363 bdb2i_check_default_attr_index_add( struct ldbminfo *li, Entry *e )
365 BDB2_TXN_HEAD *head = &li->li_txn_head;
367 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
370 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
371 if ( strcasecmp( ap->a_type, "objectclass" ))
372 bdb2i_txn_attr_config( li, ap->a_type, 1 );
378 /* check for the addition of new attribute indexes during modify */
379 /* this is called after startup of the slapd server */
380 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
381 FROM ADDING ATTRIBUTES LATER ON */
383 bdb2i_check_default_attr_index_mod( struct ldbminfo *li, LDAPModList *modlist )
385 BDB2_TXN_HEAD *head = &li->li_txn_head;
387 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
389 char *default_attrs[] = { "modifytimestamp", "modifiersname", NULL };
392 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
393 LDAPMod *mod = &ml->ml_mod;
395 if (( mod->mod_op & ~LDAP_MOD_BVALUES ) == LDAP_MOD_ADD )
396 if ( strcasecmp( mod->mod_type, "objectclass" ))
397 bdb2i_txn_attr_config( li, mod->mod_type, 1 );
400 /* these attributes are default when modifying */
401 for ( attr = 0; default_attrs[attr]; attr++ ) {
402 bdb2i_txn_attr_config( li, default_attrs[attr], 1 );
408 /* get the next ID from the NEXTID file */
410 bdb2i_get_nextid( BackendDB *be )
412 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
413 BDB2_TXN_HEAD *head = &li->li_txn_head;
417 db_recno_t rec = NEXTID_RECNO;
419 ldbm_datum_init( key );
420 ldbm_datum_init( data );
423 key.size = sizeof( rec );
425 data = bdb2i_db_fetch( head->nextidFile, key );
426 if ( data.data == NULL ) {
427 Debug( LDAP_DEBUG_ANY,
428 "next_id_read: could not get nextid from \"%s\"\n",
433 id = atol( data.data );
434 ldbm_datum_free( head->nextidFile, data );
437 Debug( LDAP_DEBUG_ANY,
438 "next_id_read %ld: return non-positive integer\n",
448 bdb2i_put_nextid( BackendDB *be, ID id )
450 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
451 BDB2_TXN_HEAD *head = &li->li_txn_head;
455 db_recno_t rec = NEXTID_RECNO;
458 sprintf( buf, "%ld\n", id );
460 ldbm_datum_init( key );
461 ldbm_datum_init( data );
464 key.size = sizeof( rec );
467 data.size = sizeof( buf );
469 flags = LDBM_REPLACE;
470 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
472 if (( rc = bdb2i_db_store( head->nextidFile, key, data, flags )) != 0 ) {
473 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): store failed (%d)\n",
482 /* BDB2 backend-private functions of libldbm */
494 memset( &dbinfo, 0, sizeof( dbinfo ));
495 if ( bdb2i_dbEnv.mp_info == NULL )
496 dbinfo.db_cachesize = dbcachesize;
497 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
498 dbinfo.db_malloc = ldbm_malloc;
500 (void) db_open( name, type, rw, mode, &bdb2i_dbEnv, &dbinfo, &ret );
507 bdb2i_db_store( LDBM ldbm, Datum key, Datum data, int flags )
511 rc = (*ldbm->put)( ldbm, txnid, &key, &data, flags & ~LDBM_SYNC );
514 if ( txnid != NULL ) {
516 /* if the store was OK, set the dirty flag,
517 otherwise set the abort flag */
524 Debug( LDAP_DEBUG_ANY,
525 "bdb2i_db_store: transaction failed: aborted.\n",
537 bdb2i_db_delete( LDBM ldbm, Datum key )
541 rc = (*ldbm->del)( ldbm, txnid, &key, 0 );
544 if ( txnid != NULL ) {
546 /* if the delete was OK, set the dirty flag,
547 otherwise set the abort flag */
554 Debug( LDAP_DEBUG_ANY,
555 "bdb2i_db_delete: transaction failed: aborted.\n",
567 bdb2i_db_fetch( LDBM ldbm, Datum key )
572 ldbm_datum_init( data );
573 data.flags = DB_DBT_MALLOC;
575 if ( (rc = (*ldbm->get)( ldbm, txnid, &key, &data, 0 )) != 0 ) {
576 if (( txnid != NULL ) && ( rc != DB_NOTFOUND )) {
578 Debug( LDAP_DEBUG_ANY,
579 "bdb2i_db_fetch: transaction failed: aborted.\n",
584 if ( data.dptr ) free( data.dptr );
594 bdb2i_db_firstkey( LDBM ldbm, DBC **dbch )
600 ldbm_datum_init( key );
601 ldbm_datum_init( data );
603 key.flags = data.flags = DB_DBT_MALLOC;
605 #if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
606 DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6
608 if ( (*ldbm->cursor)( ldbm, txnid, &dbci ))
612 if ( (*ldbm->cursor)( ldbm, txnid, &dbci, 0 ))
616 if ( txnid != NULL ) {
618 Debug( LDAP_DEBUG_ANY,
619 "bdb2i_db_firstkey: transaction failed: aborted.\n",
628 if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
629 ldbm_datum_free( ldbm, data );
631 if ( txnid != NULL ) {
633 Debug( LDAP_DEBUG_ANY,
634 "bdb2i_db_firstkey: transaction failed: aborted.\n",
639 ldbm_datum_free( ldbm, key );
651 bdb2i_db_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
656 ldbm_datum_init( data );
657 ldbm_datum_free( ldbm, key );
658 key.flags = data.flags = DB_DBT_MALLOC;
660 if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
661 ldbm_datum_free( ldbm, data );
663 if ( txnid != NULL ) {
665 Debug( LDAP_DEBUG_ANY,
666 "bdb2i_db_nextkey: transaction failed: aborted.\n",
680 /* Transaction control of write access */
681 /* Since these functions are only used by one writer at a time,
682 we do not have any concurrency (locking) problem */
684 /* initialize a new transaction */
686 bdb2i_start_transction( DB_TXNMGR *txmgr )
693 if (( rc = txn_begin( txmgr, NULL, &txnid )) != 0 ) {
695 Debug( LDAP_DEBUG_ANY, "bdb2i_start_transction failed: %d: errno=%s\n",
696 rc, strerror( err ), 0 );
699 (void) txn_abort( txnid );
703 Debug( LDAP_DEBUG_TRACE,
704 "bdb2i_start_transaction: transaction started.\n",
711 /* finish the transaction */
713 bdb2i_finish_transaction()
717 /* if transaction was NOT selected, just return */
718 if ( txnid == NULL ) return( 0 );
720 /* if nothing was wrong so far, we can try to commit the transaction */
721 /* complain, if the commit fails */
722 if (( txn_do_abort == 0 ) && ( txn_commit( txnid )) != 0 ) {
723 Debug( LDAP_DEBUG_ANY,
724 "bdb2i_finish_transaction: transaction commit failed: aborted.\n",
729 /* if anything went wrong, we have to abort the transaction */
730 if ( txn_do_abort ) {
731 Debug( LDAP_DEBUG_ANY,
732 "bdb2i_finish_transaction: transaction aborted.\n",
734 (void) txn_abort( txnid );
737 Debug( LDAP_DEBUG_TRACE,
738 "bdb2i_finish_transaction: transaction commited.\n",
742 /* XXX do NOT free the txnid memory !!! */
751 either forced (during shutdown) or when logsize or time are exceeded
752 (is called by reader and writer, so protect txn_dirty)
755 bdb2i_set_txn_checkpoint( DB_TXNMGR *txmgr, int forced )
759 /* set dirty mutex */
760 ldap_pvt_thread_mutex_lock( &txn_dirty_mutex );
768 logsize = forced ? (u_int32_t) 0 : txn_max_pending_log;
769 mins = forced ? (u_int32_t) 0 : txn_max_pending_time;
771 now = slap_get_time();
773 rc = txn_checkpoint( txmgr, logsize, mins );
775 /* if checkpointing was successful, reset txn_dirty */
777 DB_TXN_STAT *statp = NULL;
779 /* check whether the checkpoint was actually written;
780 if so, unset the txn_dirty flag */
781 if (( rc = txn_stat( txmgr, &statp, ldbm_malloc )) == 0 ) {
783 if ( statp && ( statp->st_time_ckp >= now )) {
785 Debug( LDAP_DEBUG_TRACE,
786 "bdb2i_set_txn_checkpoint succeded.\n",
792 if ( statp ) free( statp );
795 Debug( LDAP_DEBUG_ANY,
796 "bdb2i_set_txn_checkpoint: txn_stat failed: %d\n",
800 Debug( LDAP_DEBUG_ANY, "bdb2i_set_txn_checkpoint failed: %d\n",
805 /* release dirty mutex */
806 ldap_pvt_thread_mutex_unlock( &txn_dirty_mutex );