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 ) {
321 Debug( LDAP_DEBUG_ANY,
322 "bdb2i_check_additional_attr_index(): ERROR while opening datadir: %s\n",
323 strerror( err ), 0, 0 );
328 for ( file = readdir( datadir ); file; file = readdir( datadir )) {
329 char filename[MAXPATHLEN];
332 strcpy( filename, file->d_name );
333 namelen = strlen( filename );
335 if ( namelen > strlen( BDB2_SUFFIX )) {
337 if ( !strcasecmp( filename + namelen - strlen( BDB2_SUFFIX ),
340 *(filename + namelen - strlen( BDB2_SUFFIX )) = '\0';
341 bdb2i_txn_attr_config( li, filename, 0 );
343 Debug( LDAP_DEBUG_TRACE, "INDEX FILE: %s\n", filename, 0, 0 );
357 /* check for the addition of new attribute indexes during add */
358 /* this is called after startup of the slapd server */
359 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
360 FROM ADDING ATTRIBUTES LATER ON */
362 bdb2i_check_default_attr_index_add( struct ldbminfo *li, Entry *e )
364 BDB2_TXN_HEAD *head = &li->li_txn_head;
366 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
369 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
370 if ( strcasecmp( ap->a_type, "objectclass" ))
371 bdb2i_txn_attr_config( li, ap->a_type, 1 );
377 /* check for the addition of new attribute indexes during modify */
378 /* this is called after startup of the slapd server */
379 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
380 FROM ADDING ATTRIBUTES LATER ON */
382 bdb2i_check_default_attr_index_mod( struct ldbminfo *li, LDAPModList *modlist )
384 BDB2_TXN_HEAD *head = &li->li_txn_head;
386 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
388 char *default_attrs[] = { "modifytimestamp", "modifiersname", NULL };
391 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
392 LDAPMod *mod = &ml->ml_mod;
394 if (( mod->mod_op & ~LDAP_MOD_BVALUES ) == LDAP_MOD_ADD )
395 if ( strcasecmp( mod->mod_type, "objectclass" ))
396 bdb2i_txn_attr_config( li, mod->mod_type, 1 );
399 /* these attributes are default when modifying */
400 for ( attr = 0; default_attrs[attr]; attr++ ) {
401 bdb2i_txn_attr_config( li, default_attrs[attr], 1 );
407 /* get the next ID from the NEXTID file */
409 bdb2i_get_nextid( BackendDB *be )
411 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
412 BDB2_TXN_HEAD *head = &li->li_txn_head;
416 db_recno_t rec = NEXTID_RECNO;
418 ldbm_datum_init( key );
419 ldbm_datum_init( data );
422 key.size = sizeof( rec );
424 data = bdb2i_db_fetch( head->nextidFile, key );
425 if ( data.data == NULL ) {
426 Debug( LDAP_DEBUG_ANY,
427 "next_id_read: could not get nextid from \"%s\"\n",
432 id = atol( data.data );
433 ldbm_datum_free( head->nextidFile, data );
436 Debug( LDAP_DEBUG_ANY,
437 "next_id_read %ld: return non-positive integer\n",
447 bdb2i_put_nextid( BackendDB *be, ID id )
449 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
450 BDB2_TXN_HEAD *head = &li->li_txn_head;
454 db_recno_t rec = NEXTID_RECNO;
457 sprintf( buf, "%ld\n", id );
459 ldbm_datum_init( key );
460 ldbm_datum_init( data );
463 key.size = sizeof( rec );
466 data.size = sizeof( buf );
468 flags = LDBM_REPLACE;
469 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
471 if (( rc = bdb2i_db_store( head->nextidFile, key, data, flags )) != 0 ) {
472 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): store failed (%d)\n",
481 /* BDB2 backend-private functions of libldbm */
493 memset( &dbinfo, 0, sizeof( dbinfo ));
494 if ( bdb2i_dbEnv.mp_info == NULL )
495 dbinfo.db_cachesize = dbcachesize;
496 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
497 dbinfo.db_malloc = ldbm_malloc;
499 (void) db_open( name, type, rw, mode, &bdb2i_dbEnv, &dbinfo, &ret );
506 bdb2i_db_store( LDBM ldbm, Datum key, Datum data, int flags )
510 rc = (*ldbm->put)( ldbm, txnid, &key, &data, flags & ~LDBM_SYNC );
513 if ( txnid != NULL ) {
515 /* if the store was OK, set the dirty flag,
516 otherwise set the abort flag */
523 Debug( LDAP_DEBUG_ANY,
524 "bdb2i_db_store: transaction failed: aborted.\n",
536 bdb2i_db_delete( LDBM ldbm, Datum key )
540 rc = (*ldbm->del)( ldbm, txnid, &key, 0 );
543 if ( txnid != NULL ) {
545 /* if the delete was OK, set the dirty flag,
546 otherwise set the abort flag */
553 Debug( LDAP_DEBUG_ANY,
554 "bdb2i_db_delete: transaction failed: aborted.\n",
566 bdb2i_db_fetch( LDBM ldbm, Datum key )
571 ldbm_datum_init( data );
572 data.flags = DB_DBT_MALLOC;
574 if ( (rc = (*ldbm->get)( ldbm, txnid, &key, &data, 0 )) != 0 ) {
575 if (( txnid != NULL ) && ( rc != DB_NOTFOUND )) {
577 Debug( LDAP_DEBUG_ANY,
578 "bdb2i_db_fetch: transaction failed: aborted.\n",
583 if ( data.dptr ) free( data.dptr );
593 bdb2i_db_firstkey( LDBM ldbm, DBC **dbch )
599 ldbm_datum_init( key );
600 ldbm_datum_init( data );
602 key.flags = data.flags = DB_DBT_MALLOC;
604 #if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
605 DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6
607 if ( (*ldbm->cursor)( ldbm, txnid, &dbci ))
611 if ( (*ldbm->cursor)( ldbm, txnid, &dbci, 0 ))
615 if ( txnid != NULL ) {
617 Debug( LDAP_DEBUG_ANY,
618 "bdb2i_db_firstkey: transaction failed: aborted.\n",
627 if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
628 ldbm_datum_free( ldbm, data );
630 if ( txnid != NULL ) {
632 Debug( LDAP_DEBUG_ANY,
633 "bdb2i_db_firstkey: transaction failed: aborted.\n",
638 ldbm_datum_free( ldbm, key );
650 bdb2i_db_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
655 ldbm_datum_init( data );
656 ldbm_datum_free( ldbm, key );
657 key.flags = data.flags = DB_DBT_MALLOC;
659 if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
660 ldbm_datum_free( ldbm, data );
662 if ( txnid != NULL ) {
664 Debug( LDAP_DEBUG_ANY,
665 "bdb2i_db_nextkey: transaction failed: aborted.\n",
679 /* Transaction control of write access */
680 /* Since these functions are only used by one writer at a time,
681 we do not have any concurrency (locking) problem */
683 /* initialize a new transaction */
685 bdb2i_start_transction( DB_TXNMGR *txmgr )
692 if (( rc = txn_begin( txmgr, NULL, &txnid )) != 0 ) {
694 Debug( LDAP_DEBUG_ANY, "bdb2i_start_transction failed: %d: errno=%s\n",
695 rc, strerror( err ), 0 );
698 (void) txn_abort( txnid );
702 Debug( LDAP_DEBUG_TRACE,
703 "bdb2i_start_transaction: transaction started.\n",
710 /* finish the transaction */
712 bdb2i_finish_transaction()
716 /* if transaction was NOT selected, just return */
717 if ( txnid == NULL ) return( 0 );
719 /* if nothing was wrong so far, we can try to commit the transaction */
720 /* complain, if the commit fails */
721 if (( txn_do_abort == 0 ) && ( txn_commit( txnid )) != 0 ) {
722 Debug( LDAP_DEBUG_ANY,
723 "bdb2i_finish_transaction: transaction commit failed: aborted.\n",
728 /* if anything went wrong, we have to abort the transaction */
729 if ( txn_do_abort ) {
730 Debug( LDAP_DEBUG_ANY,
731 "bdb2i_finish_transaction: transaction aborted.\n",
733 (void) txn_abort( txnid );
736 Debug( LDAP_DEBUG_TRACE,
737 "bdb2i_finish_transaction: transaction commited.\n",
741 /* XXX do NOT free the txnid memory !!! */
750 either forced (during shutdown) or when logsize or time are exceeded
751 (is called by reader and writer, so protect txn_dirty)
754 bdb2i_set_txn_checkpoint( DB_TXNMGR *txmgr, int forced )
758 /* set dirty mutex */
759 ldap_pvt_thread_mutex_lock( &txn_dirty_mutex );
767 logsize = forced ? (u_int32_t) 0 : txn_max_pending_log;
768 mins = forced ? (u_int32_t) 0 : txn_max_pending_time;
770 now = slap_get_time();
772 rc = txn_checkpoint( txmgr, logsize, mins );
774 /* if checkpointing was successful, reset txn_dirty */
776 DB_TXN_STAT *statp = NULL;
778 /* check whether the checkpoint was actually written;
779 if so, unset the txn_dirty flag */
780 if (( rc = txn_stat( txmgr, &statp, ldbm_malloc )) == 0 ) {
782 if ( statp && ( statp->st_time_ckp >= now )) {
784 Debug( LDAP_DEBUG_TRACE,
785 "bdb2i_set_txn_checkpoint succeded.\n",
791 if ( statp ) free( statp );
794 Debug( LDAP_DEBUG_ANY,
795 "bdb2i_set_txn_checkpoint: txn_stat failed: %d\n",
799 Debug( LDAP_DEBUG_ANY, "bdb2i_set_txn_checkpoint failed: %d\n",
804 /* release dirty mutex */
805 ldap_pvt_thread_mutex_unlock( &txn_dirty_mutex );