~ubuntu-branches/ubuntu/precise/rpm/precise-proposed

« back to all changes in this revision

Viewing changes to db/db/db_open.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2009-06-25 18:57:20 UTC
  • mfrom: (1.1.5 upstream) (4.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090625185720-617sjskgtgmf09vf
Tags: 4.7.0-7ubuntu1
* Merge from debian unstable, remaining changes:
  - change build depends from libdwarf-dev -> libdw-dev
    (libdwarf-dev is in universe)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1996-2004
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id: db_open.c,v 11.240 2004/09/22 20:53:19 margo Exp $
8
 
 */
9
 
 
10
 
#include "db_config.h"
11
 
 
12
 
#ifndef NO_SYSTEM_INCLUDES
13
 
#include <sys/types.h>
14
 
 
15
 
#include <stdlib.h>
16
 
#include <string.h>
17
 
#endif
18
 
 
19
 
#include "db_int.h"
20
 
#include "dbinc/db_page.h"
21
 
#include "dbinc/db_shash.h"
22
 
#include "dbinc/db_swap.h"
23
 
#include "dbinc/btree.h"
24
 
#include "dbinc/crypto.h"
25
 
#include "dbinc/hmac.h"
26
 
#include "dbinc/fop.h"
27
 
#include "dbinc/hash.h"
28
 
#include "dbinc/lock.h"
29
 
#include "dbinc/log.h"
30
 
#include "dbinc/mp.h"
31
 
#include "dbinc/qam.h"
32
 
#include "dbinc/txn.h"
33
 
 
34
 
/*
35
 
 * __db_open --
36
 
 *      DB->open method.
37
 
 *
38
 
 * This routine gets called in three different ways:
39
 
 *
40
 
 * 1. It can be called to open a file/database.  In this case, subdb will
41
 
 *    be NULL and meta_pgno will be PGNO_BASE_MD.
42
 
 * 2. It can be called to open a subdatabase during normal operation.  In
43
 
 *    this case, name and subname will both be non-NULL and meta_pgno will
44
 
 *    be PGNO_BASE_MD (also PGNO_INVALID).
45
 
 * 3. It can be called during recovery to open a file/database, in which case
46
 
 *    name will be non-NULL, subname will be NULL, and meta-pgno will be
47
 
 *    PGNO_BASE_MD.
48
 
 * 4. It can be called during recovery to open a subdatabase, in which case
49
 
 *    name will be non-NULL, subname may be NULL and meta-pgno will be
50
 
 *    a valid pgno (i.e., not PGNO_BASE_MD).
51
 
 *
52
 
 * PUBLIC: int __db_open __P((DB *, DB_TXN *,
53
 
 * PUBLIC:     const char *, const char *, DBTYPE, u_int32_t, int, db_pgno_t));
54
 
 */
55
 
int
56
 
__db_open(dbp, txn, fname, dname, type, flags, mode, meta_pgno)
57
 
        DB *dbp;
58
 
        DB_TXN *txn;
59
 
        const char *fname, *dname;
60
 
        DBTYPE type;
61
 
        u_int32_t flags;
62
 
        int mode;
63
 
        db_pgno_t meta_pgno;
64
 
{
65
 
        DB_ENV *dbenv;
66
 
        int ret;
67
 
        u_int32_t id;
68
 
 
69
 
        dbenv = dbp->dbenv;
70
 
        id = TXN_INVALID;
71
 
 
72
 
        DB_TEST_RECOVERY(dbp, DB_TEST_PREOPEN, ret, fname);
73
 
 
74
 
        /*
75
 
         * If the environment was configured with threads, the DB handle
76
 
         * must also be free-threaded, so we force the DB_THREAD flag on.
77
 
         * (See SR #2033 for why this is a requirement--recovery needs
78
 
         * to be able to grab a dbp using __db_fileid_to_dbp, and it has
79
 
         * no way of knowing which dbp goes with which thread, so whichever
80
 
         * one it finds has to be usable in any of them.)
81
 
         */
82
 
        if (F_ISSET(dbenv, DB_ENV_THREAD))
83
 
                LF_SET(DB_THREAD);
84
 
 
85
 
        /* Convert any DB->open flags. */
86
 
        if (LF_ISSET(DB_RDONLY))
87
 
                F_SET(dbp, DB_AM_RDONLY);
88
 
        if (LF_ISSET(DB_DIRTY_READ))
89
 
                F_SET(dbp, DB_AM_DIRTY);
90
 
 
91
 
        if (txn != NULL)
92
 
                F_SET(dbp, DB_AM_TXN);
93
 
 
94
 
        /* Fill in the type. */
95
 
        dbp->type = type;
96
 
 
97
 
        /*
98
 
         * If fname is NULL, it's always a create, so make sure that we
99
 
         * have a type specified.  It would be nice if this checking
100
 
         * were done in __db_open where most of the interface checking
101
 
         * is done, but this interface (__db_dbopen) is used by the
102
 
         * recovery and limbo system, so we need to safeguard this
103
 
         * interface as well.
104
 
         */
105
 
        if (fname == NULL) {
106
 
                F_SET(dbp, DB_AM_INMEM);
107
 
 
108
 
                if (dbp->type == DB_UNKNOWN) {
109
 
                        __db_err(dbenv,
110
 
                            "DBTYPE of unknown without existing file");
111
 
                        return (EINVAL);
112
 
                }
113
 
 
114
 
                if (dbp->pgsize == 0)
115
 
                        dbp->pgsize = DB_DEF_IOSIZE;
116
 
 
117
 
                /*
118
 
                 * If the file is a temporary file and we're doing locking,
119
 
                 * then we have to create a unique file ID.  We can't use our
120
 
                 * normal dev/inode pair (or whatever this OS uses in place of
121
 
                 * dev/inode pairs) because no backing file will be created
122
 
                 * until the mpool cache is filled forcing the buffers to disk.
123
 
                 * Grab a random locker ID to use as a file ID.  The created
124
 
                 * ID must never match a potential real file ID -- we know it
125
 
                 * won't because real file IDs contain a time stamp after the
126
 
                 * dev/inode pair, and we're simply storing a 4-byte value.
127
 
                 *
128
 
                 * !!!
129
 
                 * Store the locker in the file id structure -- we can get it
130
 
                 * from there as necessary, and it saves having two copies.
131
 
                 */
132
 
                if (LOCKING_ON(dbenv) &&
133
 
                    (ret = __lock_id(dbenv, (u_int32_t *)dbp->fileid)) != 0)
134
 
                        return (ret);
135
 
        } else if (dname == NULL && meta_pgno == PGNO_BASE_MD) {
136
 
                /* Open/create the underlying file.  Acquire locks. */
137
 
                if ((ret =
138
 
                    __fop_file_setup(dbp, txn, fname, mode, flags, &id)) != 0)
139
 
                        return (ret);
140
 
        } else {
141
 
                if ((ret = __fop_subdb_setup(dbp,
142
 
                    txn, fname, dname, mode, flags)) != 0)
143
 
                        return (ret);
144
 
                meta_pgno = dbp->meta_pgno;
145
 
        }
146
 
 
147
 
        /*
148
 
         * If we created the file, set the truncate flag for the mpool.  This
149
 
         * isn't for anything we've done, it's protection against stupid user
150
 
         * tricks: if the user deleted a file behind Berkeley DB's back, we
151
 
         * may still have pages in the mpool that match the file's "unique" ID.
152
 
         *
153
 
         * Note that if we're opening a subdatabase, we don't want to set
154
 
         * the TRUNCATE flag even if we just created the file--we already
155
 
         * opened and updated the master using access method interfaces,
156
 
         * so we don't want to get rid of any pages that are in the mpool.
157
 
         * If we created the file when we opened the master, we already hit
158
 
         * this check in a non-subdatabase context then.
159
 
         */
160
 
        if (dname == NULL && F_ISSET(dbp, DB_AM_CREATED))
161
 
                LF_SET(DB_TRUNCATE);
162
 
 
163
 
        /* Set up the underlying environment. */
164
 
        if ((ret = __db_dbenv_setup(dbp, txn, fname, id, flags)) != 0)
165
 
                return (ret);
166
 
 
167
 
        /*
168
 
         * Set the open flag.  We use it to mean that the dbp has gone
169
 
         * through mpf setup, including dbreg_register.  Also, below,
170
 
         * the underlying access method open functions may want to do
171
 
         * things like acquire cursors, so the open flag has to be set
172
 
         * before calling them.
173
 
         */
174
 
        F_SET(dbp, DB_AM_OPEN_CALLED);
175
 
 
176
 
        /*
177
 
         * For unnamed files, we need to actually create the file now
178
 
         * that the mpool is open.
179
 
         */
180
 
        if (fname == NULL && (ret = __db_new_file(dbp, txn, NULL, NULL)) != 0)
181
 
                return (ret);
182
 
 
183
 
        switch (dbp->type) {
184
 
        case DB_BTREE:
185
 
                ret = __bam_open(dbp, txn, fname, meta_pgno, flags);
186
 
                break;
187
 
        case DB_HASH:
188
 
                ret = __ham_open(dbp, txn, fname, meta_pgno, flags);
189
 
                break;
190
 
        case DB_RECNO:
191
 
                ret = __ram_open(dbp, txn, fname, meta_pgno, flags);
192
 
                break;
193
 
        case DB_QUEUE:
194
 
                ret = __qam_open(dbp, txn, fname, meta_pgno, mode, flags);
195
 
                break;
196
 
        case DB_UNKNOWN:
197
 
                return (__db_unknown_type(dbenv, "__db_dbopen", dbp->type));
198
 
        }
199
 
        if (ret != 0)
200
 
                goto err;
201
 
 
202
 
        DB_TEST_RECOVERY(dbp, DB_TEST_POSTOPEN, ret, fname);
203
 
 
204
 
        /*
205
 
         * Unnamed files don't need handle locks, so we only have to check
206
 
         * for a handle lock downgrade or lockevent in the case of named
207
 
         * files.
208
 
         */
209
 
        if (!F_ISSET(dbp, DB_AM_RECOVER) &&
210
 
            fname != NULL && LOCK_ISSET(dbp->handle_lock)) {
211
 
                if (txn != NULL) {
212
 
                        ret = __txn_lockevent(dbenv,
213
 
                            txn, dbp, &dbp->handle_lock, dbp->lid);
214
 
                } else if (LOCKING_ON(dbenv))
215
 
                        /* Trade write handle lock for read handle lock. */
216
 
                        ret = __lock_downgrade(dbenv,
217
 
                            &dbp->handle_lock, DB_LOCK_READ, 0);
218
 
        }
219
 
DB_TEST_RECOVERY_LABEL
220
 
err:
221
 
        return (ret);
222
 
}
223
 
 
224
 
/*
225
 
 * __db_get_open_flags --
226
 
 *      Accessor for flags passed into DB->open call
227
 
 *
228
 
 * PUBLIC: int __db_get_open_flags __P((DB *, u_int32_t *));
229
 
 */
230
 
int
231
 
__db_get_open_flags(dbp, flagsp)
232
 
        DB *dbp;
233
 
        u_int32_t *flagsp;
234
 
{
235
 
        DB_ILLEGAL_BEFORE_OPEN(dbp, "DB->get_open_flags");
236
 
 
237
 
        *flagsp = dbp->open_flags;
238
 
        return (0);
239
 
}
240
 
 
241
 
/*
242
 
 * __db_new_file --
243
 
 *      Create a new database file.
244
 
 *
245
 
 * PUBLIC: int __db_new_file __P((DB *, DB_TXN *, DB_FH *, const char *));
246
 
 */
247
 
int
248
 
__db_new_file(dbp, txn, fhp, name)
249
 
        DB *dbp;
250
 
        DB_TXN *txn;
251
 
        DB_FH *fhp;
252
 
        const char *name;
253
 
{
254
 
        int ret;
255
 
 
256
 
        switch (dbp->type) {
257
 
        case DB_BTREE:
258
 
        case DB_RECNO:
259
 
                ret = __bam_new_file(dbp, txn, fhp, name);
260
 
                break;
261
 
        case DB_HASH:
262
 
                ret = __ham_new_file(dbp, txn, fhp, name);
263
 
                break;
264
 
        case DB_QUEUE:
265
 
                ret = __qam_new_file(dbp, txn, fhp, name);
266
 
                break;
267
 
        case DB_UNKNOWN:
268
 
        default:
269
 
                __db_err(dbp->dbenv,
270
 
                    "%s: Invalid type %d specified", name, dbp->type);
271
 
                ret = EINVAL;
272
 
                break;
273
 
        }
274
 
 
275
 
        DB_TEST_RECOVERY(dbp, DB_TEST_POSTLOGMETA, ret, name);
276
 
        /* Sync the file in preparation for moving it into place. */
277
 
        if (ret == 0 && fhp != NULL)
278
 
                ret = __os_fsync(dbp->dbenv, fhp);
279
 
 
280
 
        DB_TEST_RECOVERY(dbp, DB_TEST_POSTSYNC, ret, name);
281
 
 
282
 
DB_TEST_RECOVERY_LABEL
283
 
        return (ret);
284
 
}
285
 
 
286
 
/*
287
 
 * __db_init_subdb --
288
 
 *      Initialize the dbp for a subdb.
289
 
 *
290
 
 * PUBLIC: int __db_init_subdb __P((DB *, DB *, const char *, DB_TXN *));
291
 
 */
292
 
int
293
 
__db_init_subdb(mdbp, dbp, name, txn)
294
 
        DB *mdbp, *dbp;
295
 
        const char *name;
296
 
        DB_TXN *txn;
297
 
{
298
 
        DBMETA *meta;
299
 
        DB_MPOOLFILE *mpf;
300
 
        int ret, t_ret;
301
 
 
302
 
        ret = 0;
303
 
        if (!F_ISSET(dbp, DB_AM_CREATED)) {
304
 
                /* Subdb exists; read meta-data page and initialize. */
305
 
                mpf = mdbp->mpf;
306
 
                if  ((ret = __memp_fget(mpf, &dbp->meta_pgno, 0, &meta)) != 0)
307
 
                        goto err;
308
 
                ret = __db_meta_setup(mdbp->dbenv, dbp, name, meta, 0, 0);
309
 
                if ((t_ret = __memp_fput(mpf, meta, 0)) != 0 && ret == 0)
310
 
                        ret = t_ret;
311
 
                /*
312
 
                 * If __db_meta_setup found that the meta-page hadn't
313
 
                 * been written out during recovery, we can just return.
314
 
                 */
315
 
                if (ret == ENOENT)
316
 
                        ret = 0;
317
 
                goto err;
318
 
        }
319
 
 
320
 
        /* Handle the create case here. */
321
 
        switch (dbp->type) {
322
 
        case DB_BTREE:
323
 
        case DB_RECNO:
324
 
                ret = __bam_new_subdb(mdbp, dbp, txn);
325
 
                break;
326
 
        case DB_HASH:
327
 
                ret = __ham_new_subdb(mdbp, dbp, txn);
328
 
                break;
329
 
        case DB_QUEUE:
330
 
                ret = EINVAL;
331
 
                break;
332
 
        case DB_UNKNOWN:
333
 
        default:
334
 
                __db_err(dbp->dbenv,
335
 
                    "Invalid subdatabase type %d specified", dbp->type);
336
 
                return (EINVAL);
337
 
        }
338
 
 
339
 
err:    return (ret);
340
 
}
341
 
 
342
 
/*
343
 
 * __db_chk_meta --
344
 
 *      Take a buffer containing a meta-data page and check it for a checksum
345
 
 *      (and verify the checksum if necessary) and possibly decrypt it.
346
 
 *
347
 
 *      Return 0 on success, >0 (errno) on error, -1 on checksum mismatch.
348
 
 *
349
 
 * PUBLIC: int __db_chk_meta __P((DB_ENV *, DB *, DBMETA *, int));
350
 
 */
351
 
int
352
 
__db_chk_meta(dbenv, dbp, meta, do_metachk)
353
 
        DB_ENV *dbenv;
354
 
        DB *dbp;
355
 
        DBMETA *meta;
356
 
        int do_metachk;
357
 
{
358
 
        int is_hmac, ret, swapped;
359
 
        u_int32_t orig_chk;
360
 
        u_int8_t *chksum;
361
 
 
362
 
        ret = 0;
363
 
 
364
 
        if (FLD_ISSET(meta->metaflags, DBMETA_CHKSUM)) {
365
 
                if (dbp != NULL)
366
 
                        F_SET(dbp, DB_AM_CHKSUM);
367
 
 
368
 
                is_hmac = meta->encrypt_alg == 0 ? 0 : 1;
369
 
                chksum = ((BTMETA *)meta)->chksum;
370
 
 
371
 
                /*
372
 
                 * If we need to swap, the checksum function overwrites the
373
 
                 * original checksum with 0, so we need to save a copy of the
374
 
                 * original for swapping later.
375
 
                 */
376
 
                orig_chk = *(u_int32_t *)chksum;
377
 
 
378
 
                /*
379
 
                 * We cannot add this to __db_metaswap because that gets done
380
 
                 * later after we've verified the checksum or decrypted.
381
 
                 */
382
 
                if (do_metachk) {
383
 
                        swapped = 0;
384
 
chk_retry:              if ((ret = __db_check_chksum(dbenv,
385
 
                            (DB_CIPHER *)dbenv->crypto_handle, chksum, meta,
386
 
                            DBMETASIZE, is_hmac)) != 0) {
387
 
                                if (is_hmac || swapped)
388
 
                                        return (ret);
389
 
 
390
 
                                M_32_SWAP(orig_chk);
391
 
                                swapped = 1;
392
 
                                *(u_int32_t *)chksum = orig_chk;
393
 
                                goto chk_retry;
394
 
                        }
395
 
                }
396
 
        } else if (dbp != NULL)
397
 
                F_CLR(dbp, DB_AM_CHKSUM);
398
 
 
399
 
#ifdef HAVE_CRYPTO
400
 
        ret = __crypto_decrypt_meta(dbenv, dbp, (u_int8_t *)meta, do_metachk);
401
 
#endif
402
 
        return (ret);
403
 
}
404
 
 
405
 
/*
406
 
 * __db_meta_setup --
407
 
 *
408
 
 * Take a buffer containing a meta-data page and figure out if it's
409
 
 * valid, and if so, initialize the dbp from the meta-data page.
410
 
 *
411
 
 * PUBLIC: int __db_meta_setup __P((DB_ENV *,
412
 
 * PUBLIC:     DB *, const char *, DBMETA *, u_int32_t, int));
413
 
 */
414
 
int
415
 
__db_meta_setup(dbenv, dbp, name, meta, oflags, do_metachk)
416
 
        DB_ENV *dbenv;
417
 
        DB *dbp;
418
 
        const char *name;
419
 
        DBMETA *meta;
420
 
        u_int32_t oflags;
421
 
        int do_metachk;
422
 
{
423
 
        u_int32_t flags, magic;
424
 
        int ret;
425
 
 
426
 
        ret = 0;
427
 
 
428
 
        /*
429
 
         * Figure out what access method we're dealing with, and then
430
 
         * call access method specific code to check error conditions
431
 
         * based on conflicts between the found file and application
432
 
         * arguments.  A found file overrides some user information --
433
 
         * we don't consider it an error, for example, if the user set
434
 
         * an expected byte order and the found file doesn't match it.
435
 
         */
436
 
        F_CLR(dbp, DB_AM_SWAP);
437
 
        magic = meta->magic;
438
 
 
439
 
swap_retry:
440
 
        switch (magic) {
441
 
        case DB_BTREEMAGIC:
442
 
        case DB_HASHMAGIC:
443
 
        case DB_QAMMAGIC:
444
 
        case DB_RENAMEMAGIC:
445
 
                break;
446
 
        case 0:
447
 
                /*
448
 
                 * The only time this should be 0 is if we're in the
449
 
                 * midst of opening a subdb during recovery and that
450
 
                 * subdatabase had its meta-data page allocated, but
451
 
                 * not yet initialized.
452
 
                 */
453
 
                if (F_ISSET(dbp, DB_AM_SUBDB) && ((IS_RECOVERING(dbenv) &&
454
 
                    F_ISSET((DB_LOG *) dbenv->lg_handle, DBLOG_FORCE_OPEN)) ||
455
 
                    meta->pgno != PGNO_INVALID))
456
 
                        return (ENOENT);
457
 
 
458
 
                goto bad_format;
459
 
        default:
460
 
                if (F_ISSET(dbp, DB_AM_SWAP))
461
 
                        goto bad_format;
462
 
 
463
 
                M_32_SWAP(magic);
464
 
                F_SET(dbp, DB_AM_SWAP);
465
 
                goto swap_retry;
466
 
        }
467
 
 
468
 
        /*
469
 
         * We can only check the meta page if we are sure we have a meta page.
470
 
         * If it is random data, then this check can fail.  So only now can we
471
 
         * checksum and decrypt.  Don't distinguish between configuration and
472
 
         * checksum match errors here, because we haven't opened the database
473
 
         * and even a checksum error isn't a reason to panic the environment.
474
 
         */
475
 
        if ((ret = __db_chk_meta(dbenv, dbp, meta, do_metachk)) != 0) {
476
 
                if (ret == -1)
477
 
                        __db_err(dbenv,
478
 
                            "%s: metadata page checksum error", name);
479
 
                goto bad_format;
480
 
        }
481
 
 
482
 
        switch (magic) {
483
 
        case DB_BTREEMAGIC:
484
 
                if (dbp->type != DB_UNKNOWN &&
485
 
                    dbp->type != DB_RECNO && dbp->type != DB_BTREE)
486
 
                        goto bad_format;
487
 
 
488
 
                flags = meta->flags;
489
 
                if (F_ISSET(dbp, DB_AM_SWAP))
490
 
                        M_32_SWAP(flags);
491
 
                if (LF_ISSET(BTM_RECNO))
492
 
                        dbp->type = DB_RECNO;
493
 
                else
494
 
                        dbp->type = DB_BTREE;
495
 
                if ((oflags & DB_TRUNCATE) == 0 && (ret =
496
 
                    __bam_metachk(dbp, name, (BTMETA *)meta)) != 0)
497
 
                        return (ret);
498
 
                break;
499
 
        case DB_HASHMAGIC:
500
 
                if (dbp->type != DB_UNKNOWN && dbp->type != DB_HASH)
501
 
                        goto bad_format;
502
 
 
503
 
                dbp->type = DB_HASH;
504
 
                if ((oflags & DB_TRUNCATE) == 0 && (ret =
505
 
                    __ham_metachk(dbp, name, (HMETA *)meta)) != 0)
506
 
                        return (ret);
507
 
                break;
508
 
        case DB_QAMMAGIC:
509
 
                if (dbp->type != DB_UNKNOWN && dbp->type != DB_QUEUE)
510
 
                        goto bad_format;
511
 
                dbp->type = DB_QUEUE;
512
 
                if ((oflags & DB_TRUNCATE) == 0 && (ret =
513
 
                    __qam_metachk(dbp, name, (QMETA *)meta)) != 0)
514
 
                        return (ret);
515
 
                break;
516
 
        case DB_RENAMEMAGIC:
517
 
                F_SET(dbp, DB_AM_IN_RENAME);
518
 
 
519
 
                /* Copy the file's ID. */
520
 
                memcpy(dbp->fileid, ((DBMETA *)meta)->uid, DB_FILE_ID_LEN);
521
 
 
522
 
                break;
523
 
        default:
524
 
                goto bad_format;
525
 
        }
526
 
        return (0);
527
 
 
528
 
bad_format:
529
 
        if (F_ISSET(dbp, DB_AM_RECOVER))
530
 
                ret = ENOENT;
531
 
        else
532
 
                __db_err(dbenv, "%s: unexpected file type or format", name);
533
 
        return (ret == 0 ? EINVAL : ret);
534
 
}