1 /* txn.c - TP support functions of the bdb2 backend */
5 #ifdef HAVE_SYS_PARAM_H
12 bdb2i_txn_head_init( BDB2_TXN_HEAD *head )
15 BDB2_TXN_FILES **fileNodeH;
17 /* for each fixed DB file allocate a file descriptor node and
18 initialize the file's name */
19 fileNodeH = &head->dbFiles;
20 for ( dbFile = BDB2_DB_DN_FILE; dbFile <= BDB2_DB_OC_IDX_FILE; dbFile++ ) {
22 char fileName[MAXPATHLEN];
24 *fileNodeH = (BDB2_TXN_FILES *) ch_calloc( 1, sizeof( BDB2_TXN_FILES ));
25 if ( *fileNodeH == NULL ) {
27 Debug( LDAP_DEBUG_ANY, "bdb2i_txn_head_init(): out of memory!\n",
33 sprintf( fileName, "%s%s", bdb2i_fixed_filenames[dbFile], BDB2_SUFFIX );
34 (*fileNodeH)->dbc_name = ch_strdup( fileName );
36 fileNodeH = &(*fileNodeH)->next;
40 /* set defaults for checkpointing */
41 head->txn_log = BDB2_TXN_CHKP_MAX_LOG;
42 head->txn_time = BDB2_TXN_CHKP_MAX_TIME;
44 /* initialize the txn_dirty_mutex */
45 ldap_pvt_thread_mutex_init( &txn_dirty_mutex );
52 bdb2i_init_db_file_cache( struct ldbminfo *li, BDB2_TXN_FILES *fileinfo )
57 fileinfo->dbc_refcnt = 1;
59 sprintf( buf, "%s" LDAP_DIRSEP "%s",
60 li->li_directory, fileinfo->dbc_name );
62 if ( stat( buf, &st ) == 0 ) {
63 fileinfo->dbc_blksize = st.st_blksize;
65 fileinfo->dbc_blksize = DEFAULT_BLOCKSIZE;
68 fileinfo->dbc_maxids = ( fileinfo->dbc_blksize / sizeof( ID )) -
70 fileinfo->dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
71 fileinfo->dbc_maxids ) + 1;
76 /* create a DB file cache entry for a specified index attribute
77 (if not already done); the function is called during config
78 file read for all index'ed attributes; if "default" index with
79 a non-none selection is given, this is remembered for run-time
80 extension of the list of index files; the function is also
81 called before add or modify operations to check for putative
82 new "default" index files; at that time, files are also opened
85 bdb2i_txn_attr_config(
90 BDB2_TXN_HEAD *head = &li->li_txn_head;
92 /* the "attribute" 'default' is special */
93 if ( strcasecmp( attr, "default" )) {
95 /* create a new index file node, if the index is not known already */
96 BDB2_TXN_FILES **fileNodeH;
97 char fileName[MAXPATHLEN];
99 sprintf( fileName, "%s%s", attr, BDB2_SUFFIX );
101 /* search for the end of the list or a node describing
102 the current attribute */
103 for ( fileNodeH = &head->dbFiles;
104 ( *fileNodeH && strcasecmp( (*fileNodeH)->dbc_name, fileName ));
105 fileNodeH = &(*fileNodeH)->next ) {
109 /* unless we have that attribute already... */
110 if ( *fileNodeH == NULL ) {
113 Debug( LDAP_DEBUG_TRACE,
114 "bdb2i_txn_attr_config(): adding node for \"%s\"\n",
117 /* if we're out of memory, we have to see, how to exit... */
118 if ( ( *fileNodeH = p = (BDB2_TXN_FILES *)
119 ch_calloc( 1, sizeof( BDB2_TXN_FILES )) ) == NULL ) {
121 Debug( LDAP_DEBUG_ANY,
122 "bdb2i_txn_attr_config(): out of memory -- FATAL.\n",
125 /* during configuration (no files are opened)
126 we can just exit, otherwise we kill ourself and
127 hope to shutdown cleanly... */
129 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
131 exit( EXIT_FAILURE );
135 p->dbc_name = ch_strdup( fileName );
137 /* if requested for, we have to open the DB file */
138 /* BUT NOT "objectclass", 'cause that's a default index ! */
139 if ( open && strcasecmp( fileName, "objectclass" )) {
141 /* re-use filename to get the complete path */
142 sprintf( fileName, "%s" LDAP_DIRSEP "%s",
143 li->li_directory, p->dbc_name );
145 /* since we have an mpool, we should not define a cache size */
146 p->dbc_db = bdb2i_db_open( fileName, DB_TYPE,
147 LDBM_WRCREAT, li->li_mode, 0 );
149 /* if the files could not be opened, something is wrong;
151 if ( p->dbc_db == NULL ) {
153 Debug( LDAP_DEBUG_ANY,
154 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
156 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
160 bdb2i_init_db_file_cache( li, p );
162 Debug( LDAP_DEBUG_TRACE,
163 "bdb2i_txn_attr_config(): NEW INDEX FILE \"%s\"\n",
169 } else { /* it is "attribute" 'default' */
171 head->withDefIDX = BDB2_WITH_DEF_IDX;
177 /* open the NEXTID file for read/write; if it does not exist,
178 create it (access to the file must be preceeded by a rewind)
181 bdb2i_open_nextid( BackendDB *be )
183 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
184 BDB2_TXN_HEAD *head = &li->li_txn_head;
187 char fileName[MAXPATHLEN];
189 sprintf( fileName, "%s" LDAP_DIRSEP "%s",
190 li->li_directory, NEXTID_NAME );
192 /* try to open the file for read and write */
193 memset( &dbinfo, 0, sizeof( dbinfo ));
194 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
195 dbinfo.db_malloc = ldbm_malloc;
197 (void) db_open( fileName, DB_RECNO, DB_CREATE | DB_THREAD,
198 li->li_mode, &bdb2i_dbEnv, &dbinfo, &db );
202 Debug( LDAP_DEBUG_ANY,
203 "bdb2i_open_nextid: could not open \"%s\"\n",
209 /* the file is open for read/write */
210 head->nextidFile = db;
216 /* open all DB during startup of the backend (necessary due to TP)
217 additional files may be opened during slapd life-time due to
218 default indexes (must be configured in slapd.conf;
219 see bdb2i_txn_attr_config)
220 also, set the counter and timer for TP checkpointing
223 bdb2i_txn_open_files( BackendDB *be )
225 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
226 BDB2_TXN_HEAD *head = &li->li_txn_head;
227 BDB2_TXN_FILES *dbFile;
230 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
231 char fileName[MAXPATHLEN];
233 sprintf( fileName, "%s" LDAP_DIRSEP "%s",
234 li->li_directory, dbFile->dbc_name );
236 /* since we have an mpool, we should not define a cache size */
237 dbFile->dbc_db = bdb2i_db_open( fileName, DB_TYPE,
238 LDBM_WRCREAT, li->li_mode, 0 );
240 /* if the files could not be opened, something is wrong; complain */
241 if ( dbFile->dbc_db == NULL ) {
243 Debug( LDAP_DEBUG_ANY,
244 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
245 dbFile->dbc_name, 0, 0 );
250 /* initialize the file info */
251 bdb2i_init_db_file_cache( li, dbFile );
253 Debug( LDAP_DEBUG_TRACE, "bdb2i_txn_open_files(): OPEN INDEX \"%s\"\n",
254 dbFile->dbc_name, 0, 0 );
258 rc = bdb2i_open_nextid( be );
260 txn_max_pending_log = head->txn_log;
261 txn_max_pending_time = head->txn_time;
267 /* close all DB files during shutdown of the backend */
269 bdb2i_txn_close_files( BackendDB *be )
271 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
272 BDB2_TXN_HEAD *head = &li->li_txn_head;
273 BDB2_TXN_FILES *dbFile;
275 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
277 ldbm_close( dbFile->dbc_db );
281 if ( head->nextidFile )
282 ldbm_close( head->nextidFile );
287 /* get the db_cache structure associated with a specified
288 DB file (replaces the on-the-fly opening of files in cache_open()
291 bdb2i_get_db_file_cache( struct ldbminfo *li, char *name )
293 BDB2_TXN_HEAD *head = &li->li_txn_head;
294 BDB2_TXN_FILES *dbFile;
297 Debug( LDAP_DEBUG_TRACE, "bdb2i_get_db_file_cache(): looking for file %s\n",
300 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
303 if ( !strcasecmp( dbFile->dbc_name, name )) return( dbFile );
307 Debug( LDAP_DEBUG_ANY,
308 "bdb2i_get_db_file_cache(): UPS, could't find \"%s\" \n", name, 0, 0 );
310 /* ups, we couldn't find the file */
316 /* check for new attribute indexes, that might have been created
317 during former runs of slapd */
318 /* this is called during startup of the slapd server */
320 bdb2i_check_additional_attr_index( struct ldbminfo *li )
325 if ( ( datadir = opendir( li->li_directory ) ) == NULL ) {
328 Debug( LDAP_DEBUG_ANY,
329 "bdb2i_check_additional_attr_index(): ERROR while opening datadir: %s\n",
330 strerror( err ), 0, 0 );
335 for ( file = readdir( datadir ); file; file = readdir( datadir )) {
336 char filename[MAXPATHLEN];
339 strcpy( filename, file->d_name );
340 namelen = strlen( filename );
342 if ( namelen > strlen( BDB2_SUFFIX )) {
344 if ( !strcasecmp( filename + namelen - strlen( BDB2_SUFFIX ),
347 *(filename + namelen - strlen( BDB2_SUFFIX )) = '\0';
348 bdb2i_txn_attr_config( li, filename, 0 );
350 Debug( LDAP_DEBUG_TRACE, "INDEX FILE: %s\n", filename, 0, 0 );
364 /* check for the addition of new attribute indexes during add */
365 /* this is called after startup of the slapd server */
366 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
367 FROM ADDING ATTRIBUTES LATER ON */
369 bdb2i_check_default_attr_index_add( struct ldbminfo *li, Entry *e )
371 BDB2_TXN_HEAD *head = &li->li_txn_head;
373 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
376 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
377 if ( strcasecmp( ap->a_type, "objectclass" ))
378 bdb2i_txn_attr_config( li, ap->a_type, 1 );
384 /* check for the addition of new attribute indexes during modify */
385 /* this is called after startup of the slapd server */
386 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
387 FROM ADDING ATTRIBUTES LATER ON */
389 bdb2i_check_default_attr_index_mod( struct ldbminfo *li, LDAPModList *modlist )
391 BDB2_TXN_HEAD *head = &li->li_txn_head;
393 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
395 char *default_attrs[] = { "modifytimestamp", "modifiersname", NULL };
398 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
399 LDAPMod *mod = &ml->ml_mod;
401 if (( mod->mod_op & ~LDAP_MOD_BVALUES ) == LDAP_MOD_ADD )
402 if ( strcasecmp( mod->mod_type, "objectclass" ))
403 bdb2i_txn_attr_config( li, mod->mod_type, 1 );
406 /* these attributes are default when modifying */
407 for ( attr = 0; default_attrs[attr]; attr++ ) {
408 bdb2i_txn_attr_config( li, default_attrs[attr], 1 );
414 /* get the next ID from the NEXTID file */
416 bdb2i_get_nextid( BackendDB *be )
418 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
419 BDB2_TXN_HEAD *head = &li->li_txn_head;
423 db_recno_t rec = NEXTID_RECNO;
425 ldbm_datum_init( key );
426 ldbm_datum_init( data );
429 key.size = sizeof( rec );
431 data = bdb2i_db_fetch( head->nextidFile, key );
432 if ( data.data == NULL ) {
433 Debug( LDAP_DEBUG_ANY,
434 "next_id_read: could not get nextid from \"%s\"\n",
439 id = atol( data.data );
440 ldbm_datum_free( head->nextidFile, data );
443 Debug( LDAP_DEBUG_ANY,
444 "next_id_read %ld: return non-positive integer\n",
454 bdb2i_put_nextid( BackendDB *be, ID id )
456 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
457 BDB2_TXN_HEAD *head = &li->li_txn_head;
461 db_recno_t rec = NEXTID_RECNO;
464 sprintf( buf, "%ld\n", id );
466 ldbm_datum_init( key );
467 ldbm_datum_init( data );
470 key.size = sizeof( rec );
473 data.size = sizeof( buf );
475 flags = LDBM_REPLACE;
476 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
478 if (( rc = bdb2i_db_store( head->nextidFile, key, data, flags )) != 0 ) {
479 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): store failed (%d)\n",
488 /* BDB2 backend-private functions of libldbm */
500 memset( &dbinfo, 0, sizeof( dbinfo ));
501 if ( bdb2i_dbEnv.mp_info == NULL )
502 dbinfo.db_cachesize = dbcachesize;
503 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
504 dbinfo.db_malloc = ldbm_malloc;
506 (void) db_open( name, type, rw, mode, &bdb2i_dbEnv, &dbinfo, &ret );
513 bdb2i_db_store( LDBM ldbm, Datum key, Datum data, int flags )
517 rc = (*ldbm->put)( ldbm, txnid, &key, &data, flags & ~LDBM_SYNC );
520 if ( txnid != NULL ) {
522 /* if the store was OK, set the dirty flag,
523 otherwise set the abort flag */
530 Debug( LDAP_DEBUG_ANY,
531 "bdb2i_db_store: transaction failed: aborted.\n",
543 bdb2i_db_delete( LDBM ldbm, Datum key )
547 rc = (*ldbm->del)( ldbm, txnid, &key, 0 );
550 if ( txnid != NULL ) {
552 /* if the delete was OK, set the dirty flag,
553 otherwise set the abort flag */
560 Debug( LDAP_DEBUG_ANY,
561 "bdb2i_db_delete: transaction failed: aborted.\n",
573 bdb2i_db_fetch( LDBM ldbm, Datum key )
578 ldbm_datum_init( data );
579 data.flags = DB_DBT_MALLOC;
581 if ( (rc = (*ldbm->get)( ldbm, txnid, &key, &data, 0 )) != 0 ) {
582 if (( txnid != NULL ) && ( rc != DB_NOTFOUND )) {
584 Debug( LDAP_DEBUG_ANY,
585 "bdb2i_db_fetch: transaction failed: aborted.\n",
590 if ( data.dptr ) free( data.dptr );
600 bdb2i_db_firstkey( LDBM ldbm, DBC **dbch )
606 ldbm_datum_init( key );
607 ldbm_datum_init( data );
609 key.flags = data.flags = DB_DBT_MALLOC;
611 #if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
612 DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6
614 if ( (*ldbm->cursor)( ldbm, txnid, &dbci ))
618 if ( (*ldbm->cursor)( ldbm, txnid, &dbci, 0 ))
622 if ( txnid != NULL ) {
624 Debug( LDAP_DEBUG_ANY,
625 "bdb2i_db_firstkey: transaction failed: aborted.\n",
634 if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
635 ldbm_datum_free( ldbm, data );
637 if ( txnid != NULL ) {
639 Debug( LDAP_DEBUG_ANY,
640 "bdb2i_db_firstkey: transaction failed: aborted.\n",
645 ldbm_datum_free( ldbm, key );
657 bdb2i_db_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
662 ldbm_datum_init( data );
663 ldbm_datum_free( ldbm, key );
664 key.flags = data.flags = DB_DBT_MALLOC;
666 if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
667 ldbm_datum_free( ldbm, data );
669 if ( txnid != NULL ) {
671 Debug( LDAP_DEBUG_ANY,
672 "bdb2i_db_nextkey: transaction failed: aborted.\n",
686 /* Transaction control of write access */
687 /* Since these functions are only used by one writer at a time,
688 we do not have any concurrency (locking) problem */
690 /* initialize a new transaction */
692 bdb2i_start_transction( DB_TXNMGR *txmgr )
699 if (( rc = txn_begin( txmgr, NULL, &txnid )) != 0 ) {
701 Debug( LDAP_DEBUG_ANY, "bdb2i_start_transction failed: %d: errno=%s\n",
702 rc, strerror( err ), 0 );
705 (void) txn_abort( txnid );
709 Debug( LDAP_DEBUG_TRACE,
710 "bdb2i_start_transaction: transaction started.\n",
717 /* finish the transaction */
719 bdb2i_finish_transaction()
723 /* if transaction was NOT selected, just return */
724 if ( txnid == NULL ) return( 0 );
726 /* if nothing was wrong so far, we can try to commit the transaction */
727 /* complain, if the commit fails */
728 if (( txn_do_abort == 0 ) && ( txn_commit( txnid )) != 0 ) {
729 Debug( LDAP_DEBUG_ANY,
730 "bdb2i_finish_transaction: transaction commit failed: aborted.\n",
735 /* if anything went wrong, we have to abort the transaction */
736 if ( txn_do_abort ) {
737 Debug( LDAP_DEBUG_ANY,
738 "bdb2i_finish_transaction: transaction aborted.\n",
740 (void) txn_abort( txnid );
743 Debug( LDAP_DEBUG_TRACE,
744 "bdb2i_finish_transaction: transaction commited.\n",
748 /* XXX do NOT free the txnid memory !!! */
757 either forced (during shutdown) or when logsize or time are exceeded
758 (is called by reader and writer, so protect txn_dirty)
761 bdb2i_set_txn_checkpoint( DB_TXNMGR *txmgr, int forced )
765 /* set dirty mutex */
766 ldap_pvt_thread_mutex_lock( &txn_dirty_mutex );
774 logsize = forced ? (u_int32_t) 0 : txn_max_pending_log;
775 mins = forced ? (u_int32_t) 0 : txn_max_pending_time;
777 now = slap_get_time();
779 rc = txn_checkpoint( txmgr, logsize, mins );
781 /* if checkpointing was successful, reset txn_dirty */
783 DB_TXN_STAT *statp = NULL;
785 /* check whether the checkpoint was actually written;
786 if so, unset the txn_dirty flag */
787 if (( rc = txn_stat( txmgr, &statp, ldbm_malloc )) == 0 ) {
789 if ( statp && ( statp->st_time_ckp >= now )) {
791 Debug( LDAP_DEBUG_TRACE,
792 "bdb2i_set_txn_checkpoint succeded.\n",
798 if ( statp ) free( statp );
801 Debug( LDAP_DEBUG_ANY,
802 "bdb2i_set_txn_checkpoint: txn_stat failed: %d\n",
806 Debug( LDAP_DEBUG_ANY, "bdb2i_set_txn_checkpoint failed: %d\n",
811 /* release dirty mutex */
812 ldap_pvt_thread_mutex_unlock( &txn_dirty_mutex );