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 = head->dbFileHandle[dbFile] =
20 (BDB2_TXN_FILES *) ch_calloc( 1, sizeof( BDB2_TXN_FILES ));
21 if ( *fileNodeH == NULL ) {
23 Debug( LDAP_DEBUG_ANY, "bdb2i_txn_head_init(): out of memory!\n",
29 sprintf( fileName, "%s%s", bdb2i_fixed_filenames[dbFile], LDBM_SUFFIX );
30 (*fileNodeH)->dbc_name = strdup( fileName );
32 fileNodeH = &(*fileNodeH)->next;
40 bdb2i_init_db_file_cache( struct ldbminfo *li, BDB2_TXN_FILES *fileinfo )
46 pthread_mutex_lock( ¤ttime_mutex );
47 curtime = currenttime;
48 pthread_mutex_unlock( ¤ttime_mutex );
50 fileinfo->dbc_refcnt = 1;
51 fileinfo->dbc_lastref = curtime;
53 sprintf( buf, "%s%s%s", li->li_directory, DEFAULT_DIRSEP,
55 if ( stat( buf, &st ) == 0 ) {
56 fileinfo->dbc_blksize = st.st_blksize;
58 fileinfo->dbc_blksize = DEFAULT_BLOCKSIZE;
61 fileinfo->dbc_maxids = ( fileinfo->dbc_blksize / sizeof( ID )) - 2;
62 fileinfo->dbc_maxindirect = ( SLAPD_LDBM_MIN_MAXIDS /
63 fileinfo->dbc_maxids ) + 1;
69 bdb2i_txn_attr_config(
74 BDB2_TXN_HEAD *head = &li->li_txn_head;
76 /* the "attribute" 'default' is special */
77 if ( strcasecmp( attr, "default" )) {
79 /* create a new index file node, if the index is not known already */
80 BDB2_TXN_FILES **fileNodeH;
81 char fileName[MAXPATHLEN];
83 sprintf( fileName, "%s%s", attr, LDBM_SUFFIX );
85 /* search for the end of the list or a node describing
86 the current attribute */
87 for ( fileNodeH = &head->dbFiles;
88 ( *fileNodeH && strcasecmp( (*fileNodeH)->dbc_name, fileName ));
89 fileNodeH = &(*fileNodeH)->next ) {
93 /* unless we have that attribute already... */
94 if ( *fileNodeH == NULL ) {
97 Debug( LDAP_DEBUG_TRACE,
98 "bdb2i_txn_attr_config(): adding node for \"%s\"\n",
101 /* if we're out of memory, we have to see, how to exit... */
102 if ( ( *fileNodeH = p = (BDB2_TXN_FILES *)
103 ch_calloc( 1, sizeof( BDB2_TXN_FILES )) ) == NULL ) {
105 Debug( LDAP_DEBUG_ANY,
106 "bdb2i_txn_attr_config(): out of memory -- FATAL.\n",
109 /* during configuration (no files are opened)
110 we can just exit, otherwise we kill ourself and
111 hope to shutdown cleanly... */
113 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
119 p->dbc_name = strdup( fileName );
121 /* if requested for, we have to open the DB file */
122 /* BUT NOT "objectclass", 'cause that's a default index ! */
123 if ( open && strcasecmp( fileName, "objectclass" )) {
125 /* since we have an mpool, we should not define a cache size */
126 p->dbc_db = ldbm_open_env( p->dbc_name, LDBM_WRCREAT,
127 li->li_mode, 0, &li->li_db_env );
129 /* if the files could not be opened, something is wrong;
131 if ( p->dbc_db == NULL ) {
133 Debug( LDAP_DEBUG_ANY,
134 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
136 pthread_kill( pthread_self(), LDAP_SIGUSR1 );
140 bdb2i_init_db_file_cache( li, p );
142 Debug( LDAP_DEBUG_TRACE,
143 "bdb2i_txn_attr_config(): NEW INDEX FILE \"%s\"\n",
149 } else { /* it is "attribute" 'default' */
151 head->withDefIDX = BDB2_WITH_DEF_IDX;
158 bdb2i_txn_open_files( struct ldbminfo *li )
160 BDB2_TXN_HEAD *head = &li->li_txn_head;
161 BDB2_TXN_FILES *dbFile;
163 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
165 /* since we have an mpool, we should not define a cache size */
166 dbFile->dbc_db = ldbm_open_env( dbFile->dbc_name, LDBM_WRCREAT,
167 li->li_mode, 0, &li->li_db_env );
169 /* if the files could not be opened, something is wrong; complain */
170 if ( dbFile->dbc_db == NULL ) {
172 Debug( LDAP_DEBUG_ANY,
173 "bdb2i_txn_open_files(): couldn't open file \"%s\" -- FATAL.\n",
174 dbFile->dbc_name, 0, 0 );
179 /* initialize the file info */
180 bdb2i_init_db_file_cache( li, dbFile );
182 Debug( LDAP_DEBUG_TRACE, "bdb2i_txn_open_files(): OPEN INDEX \"%s\"\n",
183 dbFile->dbc_name, 0, 0 );
191 bdb2i_txn_close_files( BDB2_TXN_HEAD *head)
193 BDB2_TXN_FILES *dbFile;
195 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
197 ldbm_close( dbFile->dbc_db );
204 bdb2i_get_db_file_cache( struct ldbminfo *li, char *name )
206 BDB2_TXN_HEAD *head = &li->li_txn_head;
207 BDB2_TXN_FILES *dbFile;
210 for ( dbFile = head->dbFiles; dbFile; dbFile = dbFile->next ) {
212 if ( !strcasecmp( dbFile->dbc_name, name )) return( dbFile );
216 Debug( LDAP_DEBUG_ANY,
217 "bdb2i_get_db_file_cache(): UPS, could't find \"%s\" \n", name, 0, 0 );
219 /* ups, we couldn't find the file */
225 /* check for new attribute indexes, that might have been created
226 during former runs of slapd */
227 /* this is called during startup of the slapd server */
229 bdb2i_check_additional_attr_index( struct ldbminfo *li )
234 if ( ( datadir = opendir( li->li_directory ) ) == NULL ) {
235 /* if ( ( datadir = opendir( "/tmp" ) ) == NULL ) { */
237 Debug( LDAP_DEBUG_ANY,
238 "bdb2i_check_additional_attr_index(): ERROR while opening datadir: %s\n",
239 strerror( errno ), 0, 0 );
244 for ( file = readdir( datadir ); file; file = readdir( datadir )) {
245 char filename[MAXPATHLEN];
248 strcpy( filename, file->d_name );
249 namelen = strlen( filename );
251 if ( namelen > strlen( LDBM_SUFFIX )) {
253 if ( !strcasecmp( filename + namelen - strlen( LDBM_SUFFIX ),
256 *(filename + namelen - strlen( LDBM_SUFFIX )) = '\0';
257 bdb2i_txn_attr_config( li, filename, 0 );
259 Debug( LDAP_DEBUG_TRACE, "INDEX FILE: %s\n", filename, 0, 0 );
272 /* check for the addition of new attribute indexes during add */
273 /* this is called after startup of the slapd server */
274 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
275 FROM ADDING ATTRIBUTES LATER ON */
277 bdb2i_check_default_attr_index_add( struct ldbminfo *li, Entry *e )
279 BDB2_TXN_HEAD *head = &li->li_txn_head;
281 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
284 for ( ap = e->e_attrs; ap != NULL; ap = ap->a_next ) {
285 if ( strcasecmp( ap->a_type, "objectclass" ))
286 bdb2i_txn_attr_config( li, ap->a_type, 1 );
292 /* check for the addition of new attribute indexes during modify */
293 /* this is called after startup of the slapd server */
294 /* DON'T WORRY ABOUT ACCESS RIGHTS, THAT MIGHT PREVENT US
295 FROM ADDING ATTRIBUTES LATER ON */
297 bdb2i_check_default_attr_index_mod( struct ldbminfo *li, LDAPModList *modlist )
299 BDB2_TXN_HEAD *head = &li->li_txn_head;
301 if ( head->withDefIDX == BDB2_WITH_DEF_IDX ) {
303 char *default_attrs[] = { "modifytimestamp", "modifiersname", NULL };
306 for ( ml = modlist; ml != NULL; ml = ml->ml_next ) {
307 LDAPMod *mod = &ml->ml_mod;
309 if (( mod->mod_op & ~LDAP_MOD_BVALUES ) == LDAP_MOD_ADD )
310 if ( strcasecmp( mod->mod_type, "objectclass" ))
311 bdb2i_txn_attr_config( li, mod->mod_type, 1 );
314 /* these attributes are default when modifying */
315 for ( attr = 0; default_attrs[attr]; attr++ ) {
316 bdb2i_txn_attr_config( li, default_attrs[attr], 1 );