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" LDAP_DIRSEP "%s",
55 li->li_directory, fileinfo->dbc_name );
57 if ( stat( buf, &st ) == 0 ) {
58 fileinfo->dbc_blksize = st.st_blksize;
60 fileinfo->dbc_blksize = DEFAULT_BLOCKSIZE;
63 fileinfo->dbc_maxids = ( fileinfo->dbc_blksize / sizeof( ID )) -
65 fileinfo->dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
66 fileinfo->dbc_maxids ) + 1;
71 /* create a DB file cache entry for a specified index attribute
72 (if not already done); the function is called during config
73 file read for all index'ed attributes; if "default" index with
74 a non-none selection is given, this is remembered for run-time
75 extension of the list of index files; the function is also
76 called before add or modify operations to check for putative
77 new "default" index files; at that time, files are also opened
80 bdb2i_txn_attr_config(
85 BDB2_TXN_HEAD *head = &li->li_txn_head;
87 /* the "attribute" 'default' is special */
88 if ( strcasecmp( attr, "default" )) {
90 /* create a new index file node, if the index is not known already */
91 BDB2_TXN_FILES **fileNodeH;
92 char fileName[MAXPATHLEN];
94 sprintf( fileName, "%s%s", attr, BDB2_SUFFIX );
96 /* search for the end of the list or a node describing
97 the current attribute */
98 for ( fileNodeH = &head->dbFiles;
99 ( *fileNodeH && strcasecmp( (*fileNodeH)->dbc_name, fileName ));
100 fileNodeH = &(*fileNodeH)->next ) {
104 /* unless we have that attribute already... */
105 if ( *fileNodeH == NULL ) {
108 Debug( LDAP_DEBUG_TRACE,
109 "bdb2i_txn_attr_config(): adding node for \"%s\"\n",
112 /* if we're out of memory, we have to see, how to exit... */
113 if ( ( *fileNodeH = p = (BDB2_TXN_FILES *)
114 ch_calloc( 1, sizeof( BDB2_TXN_FILES )) ) == NULL ) {
116 Debug( LDAP_DEBUG_ANY,
117 "bdb2i_txn_attr_config(): out of memory -- FATAL.\n",
120 /* during configuration (no files are opened)
121 we can just exit, otherwise we kill ourself and
122 hope to shutdown cleanly... */
124 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
130 p->dbc_name = ch_strdup( fileName );
132 /* if requested for, we have to open the DB file */
133 /* BUT NOT "objectclass", 'cause that's a default index ! */
134 if ( open && strcasecmp( fileName, "objectclass" )) {
136 /* re-use filename to get the complete path */
137 sprintf( fileName, "%s" LDAP_DIRSEP "%s",
138 li->li_directory, p->dbc_name );
140 /* since we have an mpool, we should not define a cache size */
141 p->dbc_db = bdb2i_db_open( fileName, DB_TYPE,
142 LDBM_WRCREAT, li->li_mode, 0 );
144 /* if the files could not be opened, something is wrong;
146 if ( p->dbc_db == NULL ) {
148 Debug( LDAP_DEBUG_ANY,
149 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
151 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
155 bdb2i_init_db_file_cache( li, p );
157 Debug( LDAP_DEBUG_TRACE,
158 "bdb2i_txn_attr_config(): NEW INDEX FILE \"%s\"\n",
164 } else { /* it is "attribute" 'default' */
166 head->withDefIDX = BDB2_WITH_DEF_IDX;
172 /* open the NEXTID file for read/write; if it does not exist,
173 create it (access to the file must be preceeded by a rewind)
176 bdb2i_open_nextid( BackendDB *be )
178 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
179 BDB2_TXN_HEAD *head = &li->li_txn_head;
182 char fileName[MAXPATHLEN];
184 sprintf( fileName, "%s" LDAP_DIRSEP "%s",
185 li->li_directory, NEXTID_NAME );
187 /* try to open the file for read and write */
188 memset( &dbinfo, 0, sizeof( dbinfo ));
189 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
190 dbinfo.db_malloc = ldbm_malloc;
192 (void) db_open( fileName, DB_RECNO, DB_CREATE | DB_THREAD,
193 li->li_mode, &bdb2i_dbEnv, &dbinfo, &db );
197 Debug( LDAP_DEBUG_ANY,
198 "bdb2i_open_nextid: could not open \"%s\"\n",
204 /* the file is open for read/write */
205 head->nextidFile = db;
211 /* open all DB during startup of the backend (necessary due to TP)
212 additional files may be opened during slapd life-time due to
213 default indexes (must be configured in slapd.conf;
214 see bdb2i_txn_attr_config)
215 also, set the counter and timer for TP checkpointing
218 bdb2i_txn_open_files( BackendDB *be )
220 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
221 BDB2_TXN_HEAD *head = &li->li_txn_head;
222 BDB2_TXN_FILES *dbFile;
225 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
226 char fileName[MAXPATHLEN];
228 sprintf( fileName, "%s" LDAP_DIRSEP "%s",
229 li->li_directory, dbFile->dbc_name );
231 /* since we have an mpool, we should not define a cache size */
232 dbFile->dbc_db = bdb2i_db_open( fileName, DB_TYPE,
233 LDBM_WRCREAT, li->li_mode, 0 );
235 /* if the files could not be opened, something is wrong; complain */
236 if ( dbFile->dbc_db == NULL ) {
238 Debug( LDAP_DEBUG_ANY,
239 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
240 dbFile->dbc_name, 0, 0 );
245 /* initialize the file info */
246 bdb2i_init_db_file_cache( li, dbFile );
248 Debug( LDAP_DEBUG_TRACE, "bdb2i_txn_open_files(): OPEN INDEX \"%s\"\n",
249 dbFile->dbc_name, 0, 0 );
253 rc = bdb2i_open_nextid( be );
255 txn_max_pending_log = head->txn_log;
256 txn_max_pending_time = head->txn_time;
262 /* close all DB files during shutdown of the backend */
264 bdb2i_txn_close_files( BackendDB *be )
266 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
267 BDB2_TXN_HEAD *head = &li->li_txn_head;
268 BDB2_TXN_FILES *dbFile;
270 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
272 ldbm_close( dbFile->dbc_db );
276 if ( head->nextidFile )
277 ldbm_close( head->nextidFile );
282 /* get the db_cache structure associated with a specified
283 DB file (replaces the on-the-fly opening of files in cache_open()
286 bdb2i_get_db_file_cache( struct ldbminfo *li, char *name )
288 BDB2_TXN_HEAD *head = &li->li_txn_head;
289 BDB2_TXN_FILES *dbFile;
292 Debug( LDAP_DEBUG_TRACE, "bdb2i_get_db_file_cache(): looking for file %s\n",
295 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
298 if ( !strcasecmp( dbFile->dbc_name, name )) return( dbFile );
302 Debug( LDAP_DEBUG_ANY,
303 "bdb2i_get_db_file_cache(): UPS, could't find \"%s\" \n", name, 0, 0 );
305 /* ups, we couldn't find the file */
311 /* check for new attribute indexes, that might have been created
312 during former runs of slapd */
313 /* this is called during startup of the slapd server */
315 bdb2i_check_additional_attr_index( struct ldbminfo *li )
320 if ( ( datadir = opendir( li->li_directory ) ) == NULL ) {
323 Debug( LDAP_DEBUG_ANY,
324 "bdb2i_check_additional_attr_index(): ERROR while opening datadir: %s\n",
325 strerror( err ), 0, 0 );
330 for ( file = readdir( datadir ); file; file = readdir( datadir )) {
331 char filename[MAXPATHLEN];
334 strcpy( filename, file->d_name );
335 namelen = strlen( filename );
337 if ( namelen > strlen( BDB2_SUFFIX )) {
339 if ( !strcasecmp( filename + namelen - strlen( BDB2_SUFFIX ),
342 *(filename + namelen - strlen( BDB2_SUFFIX )) = '\0';
343 bdb2i_txn_attr_config( li, filename, 0 );
345 Debug( LDAP_DEBUG_TRACE, "INDEX FILE: %s\n", filename, 0, 0 );
359 /* check for the addition of new attribute indexes during add */
360 /* this is called after startup of the slapd server */
361 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
362 FROM ADDING ATTRIBUTES LATER ON */
364 bdb2i_check_default_attr_index_add( struct ldbminfo *li, Entry *e )
366 BDB2_TXN_HEAD *head = &li->li_txn_head;
368 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
371 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
372 if ( strcasecmp( ap->a_type, "objectclass" ))
373 bdb2i_txn_attr_config( li, ap->a_type, 1 );
379 /* check for the addition of new attribute indexes during modify */
380 /* this is called after startup of the slapd server */
381 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
382 FROM ADDING ATTRIBUTES LATER ON */
384 bdb2i_check_default_attr_index_mod( struct ldbminfo *li, LDAPModList *modlist )
386 BDB2_TXN_HEAD *head = &li->li_txn_head;
388 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
390 char *default_attrs[] = { "modifytimestamp", "modifiersname", NULL };
393 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
394 LDAPMod *mod = &ml->ml_mod;
396 if (( mod->mod_op & ~LDAP_MOD_BVALUES ) == LDAP_MOD_ADD )
397 if ( strcasecmp( mod->mod_type, "objectclass" ))
398 bdb2i_txn_attr_config( li, mod->mod_type, 1 );
401 /* these attributes are default when modifying */
402 for ( attr = 0; default_attrs[attr]; attr++ ) {
403 bdb2i_txn_attr_config( li, default_attrs[attr], 1 );
409 /* get the next ID from the NEXTID file */
411 bdb2i_get_nextid( BackendDB *be )
413 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
414 BDB2_TXN_HEAD *head = &li->li_txn_head;
418 db_recno_t rec = NEXTID_RECNO;
420 ldbm_datum_init( key );
421 ldbm_datum_init( data );
424 key.size = sizeof( rec );
426 data = bdb2i_db_fetch( head->nextidFile, key );
427 if ( data.data == NULL ) {
428 Debug( LDAP_DEBUG_ANY,
429 "next_id_read: could not get nextid from \"%s\"\n",
434 id = atol( data.data );
435 ldbm_datum_free( head->nextidFile, data );
438 Debug( LDAP_DEBUG_ANY,
439 "next_id_read %ld: return non-positive integer\n",
449 bdb2i_put_nextid( BackendDB *be, ID id )
451 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
452 BDB2_TXN_HEAD *head = &li->li_txn_head;
456 db_recno_t rec = NEXTID_RECNO;
459 sprintf( buf, "%ld\n", id );
461 ldbm_datum_init( key );
462 ldbm_datum_init( data );
465 key.size = sizeof( rec );
468 data.size = sizeof( buf );
470 flags = LDBM_REPLACE;
471 if ( li->li_dbcachewsync ) flags |= LDBM_SYNC;
473 if (( rc = bdb2i_db_store( head->nextidFile, key, data, flags )) != 0 ) {
474 Debug( LDAP_DEBUG_ANY, "next_id_write(%ld): store failed (%d)\n",
483 /* BDB2 backend-private functions of libldbm */
495 memset( &dbinfo, 0, sizeof( dbinfo ));
496 if ( bdb2i_dbEnv.mp_info == NULL )
497 dbinfo.db_cachesize = dbcachesize;
498 dbinfo.db_pagesize = DEFAULT_DB_PAGE_SIZE;
499 dbinfo.db_malloc = ldbm_malloc;
501 (void) db_open( name, type, rw, mode, &bdb2i_dbEnv, &dbinfo, &ret );
508 bdb2i_db_store( LDBM ldbm, Datum key, Datum data, int flags )
512 rc = (*ldbm->put)( ldbm, txnid, &key, &data, flags & ~LDBM_SYNC );
515 if ( txnid != NULL ) {
517 /* if the store was OK, set the dirty flag,
518 otherwise set the abort flag */
525 Debug( LDAP_DEBUG_ANY,
526 "bdb2i_db_store: transaction failed: aborted.\n",
538 bdb2i_db_delete( LDBM ldbm, Datum key )
542 rc = (*ldbm->del)( ldbm, txnid, &key, 0 );
545 if ( txnid != NULL ) {
547 /* if the delete was OK, set the dirty flag,
548 otherwise set the abort flag */
555 Debug( LDAP_DEBUG_ANY,
556 "bdb2i_db_delete: transaction failed: aborted.\n",
568 bdb2i_db_fetch( LDBM ldbm, Datum key )
573 ldbm_datum_init( data );
574 data.flags = DB_DBT_MALLOC;
576 if ( (rc = (*ldbm->get)( ldbm, txnid, &key, &data, 0 )) != 0 ) {
577 if (( txnid != NULL ) && ( rc != DB_NOTFOUND )) {
579 Debug( LDAP_DEBUG_ANY,
580 "bdb2i_db_fetch: transaction failed: aborted.\n",
585 if ( data.dptr ) free( data.dptr );
595 bdb2i_db_firstkey( LDBM ldbm, DBC **dbch )
601 ldbm_datum_init( key );
602 ldbm_datum_init( data );
604 key.flags = data.flags = DB_DBT_MALLOC;
606 #if defined( DB_VERSION_MAJOR ) && defined( DB_VERSION_MINOR ) && \
607 DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6
609 if ( (*ldbm->cursor)( ldbm, txnid, &dbci ))
613 if ( (*ldbm->cursor)( ldbm, txnid, &dbci, 0 ))
617 if ( txnid != NULL ) {
619 Debug( LDAP_DEBUG_ANY,
620 "bdb2i_db_firstkey: transaction failed: aborted.\n",
629 if ( (*dbci->c_get)( dbci, &key, &data, DB_NEXT ) == 0 ) {
630 ldbm_datum_free( ldbm, data );
632 if ( txnid != NULL ) {
634 Debug( LDAP_DEBUG_ANY,
635 "bdb2i_db_firstkey: transaction failed: aborted.\n",
640 ldbm_datum_free( ldbm, key );
652 bdb2i_db_nextkey( LDBM ldbm, Datum key, DBC *dbcp )
657 ldbm_datum_init( data );
658 ldbm_datum_free( ldbm, key );
659 key.flags = data.flags = DB_DBT_MALLOC;
661 if ( (*dbcp->c_get)( dbcp, &key, &data, DB_NEXT ) == 0 ) {
662 ldbm_datum_free( ldbm, data );
664 if ( txnid != NULL ) {
666 Debug( LDAP_DEBUG_ANY,
667 "bdb2i_db_nextkey: transaction failed: aborted.\n",
681 /* Transaction control of write access */
682 /* Since these functions are only used by one writer at a time,
683 we do not have any concurrency (locking) problem */
685 /* initialize a new transaction */
687 bdb2i_start_transction( DB_TXNMGR *txmgr )
694 if (( rc = txn_begin( txmgr, NULL, &txnid )) != 0 ) {
696 Debug( LDAP_DEBUG_ANY, "bdb2i_start_transction failed: %d: errno=%s\n",
697 rc, strerror( err ), 0 );
700 (void) txn_abort( txnid );
704 Debug( LDAP_DEBUG_TRACE,
705 "bdb2i_start_transaction: transaction started.\n",
712 /* finish the transaction */
714 bdb2i_finish_transaction()
718 /* if transaction was NOT selected, just return */
719 if ( txnid == NULL ) return( 0 );
721 /* if nothing was wrong so far, we can try to commit the transaction */
722 /* complain, if the commit fails */
723 if (( txn_do_abort == 0 ) && ( txn_commit( txnid )) != 0 ) {
724 Debug( LDAP_DEBUG_ANY,
725 "bdb2i_finish_transaction: transaction commit failed: aborted.\n",
730 /* if anything went wrong, we have to abort the transaction */
731 if ( txn_do_abort ) {
732 Debug( LDAP_DEBUG_ANY,
733 "bdb2i_finish_transaction: transaction aborted.\n",
735 (void) txn_abort( txnid );
738 Debug( LDAP_DEBUG_TRACE,
739 "bdb2i_finish_transaction: transaction commited.\n",
743 /* XXX do NOT free the txnid memory !!! */
752 either forced (during shutdown) or when logsize or time are exceeded
753 (is called by reader and writer, so protect txn_dirty)
756 bdb2i_set_txn_checkpoint( DB_TXNMGR *txmgr, int forced )
760 /* set dirty mutex */
761 ldap_pvt_thread_mutex_lock( &txn_dirty_mutex );
769 logsize = forced ? (u_int32_t) 0 : txn_max_pending_log;
770 mins = forced ? (u_int32_t) 0 : txn_max_pending_time;
772 now = slap_get_time();
774 rc = txn_checkpoint( txmgr, logsize, mins );
776 /* if checkpointing was successful, reset txn_dirty */
778 DB_TXN_STAT *statp = NULL;
780 /* check whether the checkpoint was actually written;
781 if so, unset the txn_dirty flag */
782 if (( rc = txn_stat( txmgr, &statp, ldbm_malloc )) == 0 ) {
784 if ( statp && ( statp->st_time_ckp >= now )) {
786 Debug( LDAP_DEBUG_TRACE,
787 "bdb2i_set_txn_checkpoint succeded.\n",
793 if ( statp ) free( statp );
796 Debug( LDAP_DEBUG_ANY,
797 "bdb2i_set_txn_checkpoint: txn_stat failed: %d\n",
801 Debug( LDAP_DEBUG_ANY, "bdb2i_set_txn_checkpoint failed: %d\n",
806 /* release dirty mutex */
807 ldap_pvt_thread_mutex_unlock( &txn_dirty_mutex );