summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/back-ldbm/upgrade.c
blob: 083b5ad5da0aa8ae513f3e9a8e743a13f70ea1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif


/* upgrade.c --- upgrade from a previous version of the database */

#include "back-ldbm.h"

/*
 * ldbm_compat_versions holds DBVERSION strings for all versions of the
 * database with which we are (upwards) compatible.  If check_db_version
 * encounters a database with a version that is not listed in this array,
 * we display a warning message.
 */

db_upgrade_info ldbm_version_suss[] = {
    /* for bdb/#.#/..., we don't have to put the version number in the 2nd col
       since DBVERSION keeps it */
    {BDB_IMPL, 0, 0, DBVERSION_NEW_IDL, DBVERSION_NO_UPGRADE},
    {LDBM_VERSION, 4, 2, DBVERSION_NEW_IDL, DBVERSION_NO_UPGRADE},
    {LDBM_VERSION_OLD, 4, 2, DBVERSION_OLD_IDL, DBVERSION_NO_UPGRADE}, 
    {LDBM_VERSION_62, 4, 2, DBVERSION_OLD_IDL, DBVERSION_NO_UPGRADE}, 
    {LDBM_VERSION_61, 3, 3, DBVERSION_OLD_IDL, DBVERSION_UPGRADE_3_4}, 
    {LDBM_VERSION_60, 3, 3, DBVERSION_OLD_IDL, DBVERSION_UPGRADE_3_4}, 
    {NULL,0,0}
};


/* clear the following flag to suppress "database files do not exist" warning
int ldbm_warn_if_no_db = 0;
*/
/* global LDBM version in the db home */

int
lookup_dbversion(char *dbversion, int flag)
{
    int i, matched = 0;
    int rval = DBVERSION_NO_UPGRADE;

    for ( i = 0; ldbm_version_suss[i].old_version_string != NULL; ++i )
    {
        if (PL_strncasecmp(dbversion, ldbm_version_suss[i].old_version_string,
                          strlen(ldbm_version_suss[i].old_version_string)) == 0)
        {
            matched = 1;
            break;
        }
    }
    if ( matched )
    {
        if ( flag & DBVERSION_TYPE )
        {
            rval |= ldbm_version_suss[i].type;
        }
        if ( flag & DBVERSION_ACTION )
        {
            int dbmajor = 0, dbminor = 0;
            if (0 == ldbm_version_suss[i].old_dbversion_major)
            {
                /* case of bdb/#.#/... */
                char *p = strchr(dbversion, '/');
                char *endp = dbversion + strlen(dbversion);
                if (NULL != p && p < endp)
                {
                    char *dotp = strchr(++p, '.');
                    if (NULL != dotp)
                    {
                        *dotp = '\0';
                        dbmajor = strtol(p, (char **)NULL, 10);
                        dbminor = strtol(++dotp, (char **)NULL, 10);
                    }
                    else
                    {
                        dbmajor = strtol(p, (char **)NULL, 10);
                    }
                }
            }
            else
            {
                dbmajor = ldbm_version_suss[i].old_dbversion_major;
                dbminor = ldbm_version_suss[i].old_dbversion_minor;
            }
            if (dbmajor < DB_VERSION_MAJOR)
            {
                /* 3.3 -> 4.x */
                rval |= ldbm_version_suss[i].action;
            }
            else if (dbminor < DB_VERSION_MINOR)
            {
                /* 4.low -> 4.high */
                rval |= DBVERSION_UPGRADE_4_4;
            }
        }
    }
    return rval;
}

/*
 * this function reads the db/DBVERSION file and check
 * 1) if the db version is supported, and
 * 2) if the db version requires some migration operation
 *
 * return: 0: supported
 *         DBVERSION_NOT_SUPPORTED: not supported
 *
 * action: 0: nothing is needed
 *         DBVERSION_UPGRADE_3_4: db3->db4 uprev is needed
 *         DBVERSION_UPGRADE_4_4: db4->db4 uprev is needed
 */
int
check_db_version( struct ldbminfo *li, int *action )
{
    int value = 0;
    char *ldbmversion = NULL;
    char *dataversion = NULL;

    *action = 0;
    dbversion_read(li, li->li_directory, &ldbmversion, &dataversion);
    if (NULL == ldbmversion || '\0' == *ldbmversion) {
        slapi_ch_free_string(&dataversion);
        return 0;
    }

    value = lookup_dbversion( ldbmversion, DBVERSION_TYPE | DBVERSION_ACTION );
    if ( !value )
    {
        LDAPDebug( LDAP_DEBUG_ANY,
           "ERROR: Database version mismatch (expecting "
           "'%s' but found '%s' in directory %s)\n",
            LDBM_VERSION, ldbmversion, li->li_directory );
        /*
         * A non-zero return here will cause slapd to exit during startup.
         */
        slapi_ch_free_string(&ldbmversion);
        slapi_ch_free_string(&dataversion);
        return DBVERSION_NOT_SUPPORTED;
    }
    if ( value & DBVERSION_UPGRADE_3_4 )
    {
        dblayer_set_recovery_required(li);
        *action = DBVERSION_UPGRADE_3_4;
    }
	else if ( value & DBVERSION_UPGRADE_4_4 )
    {
        dblayer_set_recovery_required(li);
        *action = DBVERSION_UPGRADE_4_4;
    }
    slapi_ch_free_string(&ldbmversion);
    slapi_ch_free_string(&dataversion);
    return 0;
}

/*
 * this function reads the db/<inst>/DBVERSION file and check
 * 1) if the db version is supported, and
 * 2) if the db version matches the idl configuration
 *    (nsslapd-idl-switch: new|old)
 *    note that old idl will disappear from the next major update (6.5? 7.0?)
 *
 * return: 0: supported and the version matched
 *         DBVERSION_NEED_IDL_OLD2NEW: old->new uprev is needed
 *                                     (used in convindices)
 *         DBVERSION_NEED_IDL_NEW2OLD: old db is found, for the new idl config
 *         DBVERSION_NOT_SUPPORTED: not supported
 *
 *         DBVERSION_UPGRADE_3_4: db3->db4 uprev is needed
 *         DBVERSION_UPGRADE_4_4: db4->db4 uprev is needed
 */
int
check_db_inst_version( ldbm_instance *inst )
{
    int value = 0;
    char *ldbmversion = NULL;
    char *dataversion = NULL;
    int rval = 0;
    char inst_dir[MAXPATHLEN*2];
    char *inst_dirp = NULL;

    inst_dirp =
        dblayer_get_full_inst_dir(inst->inst_li, inst, inst_dir, MAXPATHLEN*2);

    dbversion_read(inst->inst_li, inst_dirp, &ldbmversion, &dataversion);
    if (NULL == ldbmversion || '\0' == *ldbmversion) {
        return rval;
    }
    
    value = lookup_dbversion( ldbmversion, DBVERSION_TYPE | DBVERSION_ACTION );
    if ( !value )
    {
        LDAPDebug( LDAP_DEBUG_ANY,
           "ERROR: Database version mismatch (expecting "
           "'%s' but found '%s' in directory %s)\n",
            LDBM_VERSION, ldbmversion, inst->inst_dir_name );
        /*
         * A non-zero return here will cause slapd to exit during startup.
         */
        slapi_ch_free_string(&ldbmversion);
        slapi_ch_free_string(&dataversion);
        return DBVERSION_NOT_SUPPORTED;
    }

    /* recognize the difference between an old/new database regarding idl
     * (406922) */
    if (idl_get_idl_new() && !(value & DBVERSION_NEW_IDL) )
    {
        rval |= DBVERSION_NEED_IDL_OLD2NEW;
    }
    else if (!idl_get_idl_new() && !(value & DBVERSION_OLD_IDL) )
    {
        rval |= DBVERSION_NEED_IDL_NEW2OLD;
    }
    if ( value & DBVERSION_UPGRADE_3_4 )
    {
        rval |= DBVERSION_UPGRADE_3_4;
    }
	else if ( value & DBVERSION_UPGRADE_4_4 )
    {
        rval |= DBVERSION_UPGRADE_4_4;
    }
    if (inst_dirp != inst_dir)
        slapi_ch_free_string(&inst_dirp);
    slapi_ch_free_string(&ldbmversion);
    slapi_ch_free_string(&dataversion);
    return rval;
}

/*
 * adjust_idl_switch
 * if the current nsslapd-idl-switch is different from ldbmversion,
 * update the value of nsslapd-idl-switch (in LDBM_CONFIG_ENTRY)
 */
int
adjust_idl_switch(char *ldbmversion, struct ldbminfo *li)
{
    int rval = 0;

    li->li_flags |= LI_FORCE_MOD_CONFIG;
	if ((0 == PL_strncasecmp(ldbmversion, BDB_IMPL, strlen(BDB_IMPL))) ||
	    (0 == PL_strcmp(ldbmversion, LDBM_VERSION)))    /* db: new idl */
    {
        if (!idl_get_idl_new())   /* config: old idl */
        {
            replace_ldbm_config_value(CONFIG_IDL_SWITCH, "new", li);
            LDAPDebug(LDAP_DEBUG_ANY, 
                "Warning: Dbversion %s does not meet nsslapd-idl-switch: \"old\"; "
                "nsslapd-idl-switch is updated to \"new\"\n",

                ldbmversion, 0, 0);
        }
    }
    else if ((0 == strcmp(ldbmversion, LDBM_VERSION_OLD)) ||
             (0 == PL_strcmp(ldbmversion, LDBM_VERSION_61)) ||
             (0 == PL_strcmp(ldbmversion, LDBM_VERSION_62)) ||
             (0 == strcmp(ldbmversion, LDBM_VERSION_60)))    /* db: old */
    {
        if (idl_get_idl_new())   /* config: new */
        {
            replace_ldbm_config_value(CONFIG_IDL_SWITCH, "old", li);
            LDAPDebug(LDAP_DEBUG_ANY, 
                "Warning: Dbversion %s does not meet nsslapd-idl-switch: \"new\"; "
                "nsslapd-idl-switch is updated to \"old\"\n",
                ldbmversion, 0, 0);
        }
    }
    else
    {
         LDAPDebug(LDAP_DEBUG_ANY, 
                   "Warning: Dbversion %s is not supported\n", 
                   ldbmversion, 0, 0);
         rval = 1;
    }

    /* ldbminfo is a common resource; should clean up when the job is done */
    li->li_flags &= ~LI_FORCE_MOD_CONFIG;
    return rval;
}

/* Do the work to upgrade a database if needed */
/* When we're called, the database files have been opened, and any
recovery needed has been performed. */
int ldbm_upgrade(ldbm_instance *inst, int action)
{
    int rval = 0;

    if (0 == action)
    {
        return rval;
    }

    if (action & DBVERSION_UPGRADE_3_4)    /* upgrade from db3 to db4 */
    {
        /* basically, db4 supports db3.
         * so, what we need to do is rename XXX.db3 to XXX.db4 */

        int rval = dblayer_update_db_ext(inst, LDBM_SUFFIX_OLD, LDBM_SUFFIX);
        if (0 == rval)
        {
            if (idl_get_idl_new())
            {
                 LDAPDebug(LDAP_DEBUG_ANY, 
                   "ldbm_upgrade: Upgrading instance %s to %s%s is successfully done.\n",
                   inst->inst_name, LDBM_VERSION_BASE, DS_PACKAGE_VERSION);
            }
            else
            {
                 LDAPDebug(LDAP_DEBUG_ANY, 
                   "ldbm_upgrade: Upgrading instance %s to %s%s is successfully done.\n",
                   inst->inst_name, LDBM_VERSION_OLD, 0);
            }
        }
        else
        {
            /* recovery effort ... */
            dblayer_update_db_ext(inst, LDBM_SUFFIX, LDBM_SUFFIX_OLD);
            return rval;
        }
    }

    return 0; /* Means that the database is new */
}

/* Here's the upgrade process : 
    Delete all the keys from the parentid index
    Scan the id2entry file:
        Remove any hassubordinates attribute present
        Update the parentid index, maintaining a hash of high-count parents
    Scan the newly created parentid index updating the subordinatecount attributes.

    Most of the functionality is implemented in the import code.
 */
#if 0
static int upgrade_db_3x_40(backend *be)
{
    struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
    int ret = 0;
    back_txn        txn;

    static char* indexes_modified[] = {"parentid", "numsubordinates", NULL};

    LDAPDebug( LDAP_DEBUG_ANY, "WARNING: Detected a database older than this server, upgrading data...\n",0,0,0);

    dblayer_txn_init(li,&txn);
    ret = dblayer_txn_begin(li,NULL,&txn);
    if (0 != ret) {
        ldbm_nasty(filename,69,ret);
        goto error;
    }
    ret = indexfile_delete_all_keys(be,"parentid",&txn);
    if (0 != ret) {
        ldbm_nasty(filename,70,ret);
        goto error;
    }

    {
        Slapi_Mods smods;
           slapi_mods_init(&smods,1);
        /* Mods are to remove the hassubordinates attribute */
        slapi_mods_add(&smods, LDAP_MOD_DELETE, "hassubordinates", 0, NULL);
        /* This function takes care of generating the subordinatecount attribute and indexing it */
        ret = indexfile_primary_modifyall(be,slapi_mods_get_ldapmods_byref(&smods),indexes_modified,&txn);
        slapi_mods_done(&smods);
    }

    if (0 != ret) {
        ldbm_nasty(filename,61,ret);
    }

error:
    if (0 != ret ) {
        dblayer_txn_abort(li,&txn);
    } else {
        ret = dblayer_txn_commit(li,&txn);
        if (0 != ret) {
            ldbm_nasty(filename,60,ret);
        } else {
            /* Now update DBVERSION file */
        }
    }
    if (0 == ret) {
        LDAPDebug( LDAP_DEBUG_ANY, "...upgrade complete.\n",0,0,0);
    } else {
        LDAPDebug( LDAP_DEBUG_ANY, "ERROR: Attempt to upgrade the older database FAILED.\n",0,0,0);
    }
    return ret;
}

#endif