00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "config.h"
00044
00045 #ifndef lint
00046 static const char revid[] = "$Id: bt__open_8c-source.html,v 1.1 2008/06/08 10:13:37 sebdiaz Exp $";
00047 #endif
00048
00049 #ifndef NO_SYSTEM_INCLUDES
00050 #include <sys/types.h>
00051
00052 #include <errno.h>
00053 #include <limits.h>
00054 #include <string.h>
00055 #endif
00056
00057 #include "db_int.h"
00058 #include "db_page.h"
00059 #include "db_swap.h"
00060 #include "btree.h"
00061 #include "db_shash.h"
00062 #include "lock.h"
00063 #include "log.h"
00064 #include "mp.h"
00065
00066
00067
00068
00069
00070
00071
00072 int
00073 CDB___bam_open(dbp, name, base_pgno, flags)
00074 DB *dbp;
00075 const char *name;
00076 db_pgno_t base_pgno;
00077 u_int32_t flags;
00078 {
00079 BTREE *t;
00080
00081 t = dbp->bt_internal;
00082
00083
00084 dbp->del = CDB___bam_delete;
00085 dbp->key_range = CDB___bam_key_range;
00086 dbp->stat = CDB___bam_stat;
00087
00088
00089
00090
00091
00092
00093 if (t->bt_compare == CDB___bam_defcmp && t->bt_prefix != CDB___bam_defpfx) {
00094 CDB___db_err(dbp->dbenv,
00095 "prefix comparison may not be specified for default comparison routine");
00096 return (EINVAL);
00097 }
00098
00099
00100 return (CDB___bam_read_root(dbp, name, base_pgno, flags));
00101 }
00102
00103
00104
00105
00106
00107
00108 int
00109 CDB___bam_metachk(dbp, name, btm)
00110 DB *dbp;
00111 const char *name;
00112 BTMETA *btm;
00113 {
00114 DB_ENV *dbenv;
00115 u_int32_t vers;
00116 int ret;
00117
00118 dbenv = dbp->dbenv;
00119
00120
00121
00122
00123
00124 vers = btm->dbmeta.version;
00125 if (F_ISSET(dbp, DB_AM_SWAP))
00126 M_32_SWAP(vers);
00127 switch (vers) {
00128 case 6:
00129 case 7:
00130 CDB___db_err(dbenv,
00131 "%s: btree version %lu requires a version upgrade",
00132 name, (u_long)vers);
00133 return (DB_OLD_VERSION);
00134 case 8:
00135 break;
00136 default:
00137 CDB___db_err(dbenv,
00138 "%s: unsupported btree version: %lu", name, (u_long)vers);
00139 return (EINVAL);
00140 }
00141
00142
00143 if (F_ISSET(dbp, DB_AM_SWAP) && (ret = CDB___bam_mswap((PAGE *)btm)) != 0)
00144 return (ret);
00145
00146
00147
00148
00149
00150 if ((ret =
00151 CDB___db_fchk(dbenv, "DB->open", btm->dbmeta.flags, BTM_MASK)) != 0)
00152 return (ret);
00153
00154 if (F_ISSET(&btm->dbmeta, BTM_RECNO)) {
00155 if (dbp->type == DB_BTREE)
00156 goto wrong_type;
00157 dbp->type = DB_RECNO;
00158 DB_ILLEGAL_METHOD(dbp, DB_OK_RECNO);
00159 } else {
00160 if (dbp->type == DB_RECNO)
00161 goto wrong_type;
00162 dbp->type = DB_BTREE;
00163 DB_ILLEGAL_METHOD(dbp, DB_OK_BTREE);
00164 }
00165
00166 if (F_ISSET(&btm->dbmeta, BTM_DUP))
00167 F_SET(dbp, DB_AM_DUP);
00168 else
00169 if (F_ISSET(dbp, DB_AM_DUP)) {
00170 CDB___db_err(dbenv,
00171 "%s: DB_DUP specified to open method but not set in database",
00172 name);
00173 return (EINVAL);
00174 }
00175
00176 if (F_ISSET(&btm->dbmeta, BTM_RECNUM)) {
00177 if (dbp->type != DB_BTREE)
00178 goto wrong_type;
00179 F_SET(dbp, DB_BT_RECNUM);
00180
00181 if ((ret = CDB___db_fcchk(dbenv,
00182 "DB->open", dbp->flags, DB_AM_DUP, DB_BT_RECNUM)) != 0)
00183 return (ret);
00184 } else
00185 if (F_ISSET(dbp, DB_BT_RECNUM)) {
00186 CDB___db_err(dbenv,
00187 "%s: DB_RECNUM specified to open method but not set in database",
00188 name);
00189 return (EINVAL);
00190 }
00191
00192 if (F_ISSET(&btm->dbmeta, BTM_FIXEDLEN)) {
00193 if (dbp->type != DB_RECNO)
00194 goto wrong_type;
00195 F_SET(dbp, DB_RE_FIXEDLEN);
00196 } else
00197 if (F_ISSET(dbp, DB_RE_FIXEDLEN)) {
00198 CDB___db_err(dbenv,
00199 "%s: DB_FIXEDLEN specified to open method but not set in database",
00200 name);
00201 return (EINVAL);
00202 }
00203
00204 if (F_ISSET(&btm->dbmeta, BTM_RENUMBER)) {
00205 if (dbp->type != DB_RECNO)
00206 goto wrong_type;
00207 F_SET(dbp, DB_RE_RENUMBER);
00208 } else
00209 if (F_ISSET(dbp, DB_RE_RENUMBER)) {
00210 CDB___db_err(dbenv,
00211 "%s: DB_RENUMBER specified to open method but not set in database",
00212 name);
00213 return (EINVAL);
00214 }
00215
00216 if (F_ISSET(&btm->dbmeta, BTM_SUBDB))
00217 F_SET(dbp, DB_AM_SUBDB);
00218 else
00219 if (F_ISSET(dbp, DB_AM_SUBDB)) {
00220 CDB___db_err(dbenv,
00221 "%s: multiple databases specified but not supported by file",
00222 name);
00223 return (EINVAL);
00224 }
00225
00226 if (F_ISSET(&btm->dbmeta, BTM_DUPSORT)) {
00227 if (dbp->dup_compare == NULL)
00228 dbp->dup_compare = CDB___bam_defcmp;
00229 F_SET(dbp, DB_AM_DUPSORT);
00230 } else
00231 if (dbp->dup_compare != NULL) {
00232 CDB___db_err(dbenv,
00233 "%s: duplicate sort specified but not supported in database",
00234 name);
00235 return (EINVAL);
00236 }
00237
00238
00239 dbp->pgsize = btm->dbmeta.pagesize;
00240
00241
00242 memcpy(dbp->fileid, btm->dbmeta.uid, DB_FILE_ID_LEN);
00243
00244 return (0);
00245
00246 wrong_type:
00247 if (dbp->type == DB_BTREE)
00248 CDB___db_err(dbenv,
00249 "open method type is Btree, database type is Recno");
00250 else
00251 CDB___db_err(dbenv,
00252 "open method type is Recno, database type is Btree");
00253 return (EINVAL);
00254 }
00255
00256
00257
00258
00259
00260
00261
00262 int
00263 CDB___bam_read_root(dbp, name, base_pgno, flags)
00264 DB *dbp;
00265 const char *name;
00266 db_pgno_t base_pgno;
00267 u_int32_t flags;
00268 {
00269 BTMETA *meta;
00270 BTREE *t;
00271 DBC *dbc;
00272 DB_LSN orig_lsn;
00273 DB_LOCK metalock;
00274 PAGE *root;
00275 int locked, ret, t_ret;
00276
00277 ret = 0;
00278 t = dbp->bt_internal;
00279 meta = NULL;
00280 root = NULL;
00281 locked = 0;
00282
00283
00284
00285
00286
00287
00288
00289 if ((ret = dbp->cursor(dbp, dbp->open_txn,
00290 &dbc, LF_ISSET(DB_CREATE) && LOCKING(dbp->dbenv) ?
00291 DB_WRITECURSOR : 0)) != 0)
00292 return (ret);
00293
00294
00295 if ((ret =
00296 CDB___db_lget(dbc, 0, base_pgno, DB_LOCK_READ, 0, &metalock)) != 0)
00297 goto err;
00298 if ((ret = CDB_memp_fget(
00299 dbp->mpf, &base_pgno, DB_MPOOL_CREATE, (PAGE **)&meta)) != 0)
00300 goto err;
00301
00302
00303
00304
00305
00306
00307 again: if (meta->dbmeta.magic != 0) {
00308 t->bt_maxkey = meta->maxkey;
00309 t->bt_minkey = meta->minkey;
00310 t->re_pad = meta->re_pad;
00311 t->re_len = meta->re_len;
00312
00313 t->bt_meta = base_pgno;
00314 t->bt_root = meta->root;
00315
00316 (void)CDB_memp_fput(dbp->mpf, meta, 0);
00317 meta = NULL;
00318 goto done;
00319 }
00320
00321
00322 if (IS_RECOVERING(dbp->dbenv))
00323 goto done;
00324
00325
00326 if (LOCKING(dbp->dbenv)) {
00327
00328
00329
00330
00331 DB_ASSERT(LF_ISSET(DB_CREATE));
00332 if ((ret = CDB_lock_get(dbp->dbenv, dbc->locker, DB_LOCK_UPGRADE,
00333 &dbc->lock_dbt, DB_LOCK_WRITE, &dbc->mylock)) != 0)
00334 goto err;
00335 }
00336
00337
00338
00339
00340
00341 if (locked == 0 && STD_LOCKING(dbc)) {
00342 if ((ret = __LPUT(dbc, metalock)) != 0)
00343 goto err;
00344 if ((ret = CDB___db_lget(dbc,
00345 0, base_pgno, DB_LOCK_WRITE, 0, &metalock)) != 0)
00346 goto err;
00347 locked = 1;
00348 goto again;
00349 }
00350
00351
00352 orig_lsn = meta->dbmeta.lsn;
00353 memset(meta, 0, sizeof(BTMETA));
00354 meta->dbmeta.lsn = orig_lsn;
00355 meta->dbmeta.pgno = base_pgno;
00356 meta->dbmeta.magic = DB_BTREEMAGIC;
00357 meta->dbmeta.version = DB_BTREEVERSION;
00358 meta->dbmeta.pagesize = dbp->pgsize;
00359 meta->dbmeta.type = P_BTREEMETA;
00360 meta->dbmeta.free = PGNO_INVALID;
00361 if (F_ISSET(dbp, DB_AM_DUP))
00362 F_SET(&meta->dbmeta, BTM_DUP);
00363 if (F_ISSET(dbp, DB_RE_FIXEDLEN))
00364 F_SET(&meta->dbmeta, BTM_FIXEDLEN);
00365 if (F_ISSET(dbp, DB_BT_RECNUM))
00366 F_SET(&meta->dbmeta, BTM_RECNUM);
00367 if (F_ISSET(dbp, DB_RE_RENUMBER))
00368 F_SET(&meta->dbmeta, BTM_RENUMBER);
00369 if (F_ISSET(dbp, DB_AM_SUBDB))
00370 F_SET(&meta->dbmeta, BTM_SUBDB);
00371 if (dbp->dup_compare != NULL)
00372 F_SET(&meta->dbmeta, BTM_DUPSORT);
00373 if (dbp->type == DB_RECNO)
00374 F_SET(&meta->dbmeta, BTM_RECNO);
00375 memcpy(meta->dbmeta.uid, dbp->fileid, DB_FILE_ID_LEN);
00376
00377 meta->maxkey = t->bt_maxkey;
00378 meta->minkey = t->bt_minkey;
00379 meta->re_len = t->re_len;
00380 meta->re_pad = t->re_pad;
00381
00382
00383 if ((ret = CDB___db_log_page(dbp,
00384 name, &orig_lsn, base_pgno, (PAGE *)meta)) != 0)
00385 goto err;
00386
00387
00388 if ((ret = CDB___db_new(dbc,
00389 ((dbp->type == DB_RECNO ? P_LRECNO : P_LBTREE) | dbp->tags), &root)) != 0)
00390 goto err;
00391 root->level = LEAFLEVEL;
00392
00393 if (dbp->open_txn != NULL && (ret = CDB___bam_root_log(dbp->dbenv,
00394 dbp->open_txn, &meta->dbmeta.lsn, 0, dbp->log_fileid,
00395 meta->dbmeta.pgno, root->pgno, &meta->dbmeta.lsn)) != 0)
00396 goto err;
00397
00398 meta->root = root->pgno;
00399
00400 DB_TEST_RECOVERY(dbp, DB_TEST_POSTLOGMETA, ret, name);
00401 if ((ret = CDB___db_log_page(dbp,
00402 name, &root->lsn, root->pgno, root)) != 0)
00403 goto err;
00404 DB_TEST_RECOVERY(dbp, DB_TEST_POSTLOG, ret, name);
00405
00406 t->bt_meta = base_pgno;
00407 t->bt_root = root->pgno;
00408
00409
00410 if ((ret = CDB_memp_fput(dbp->mpf, meta, DB_MPOOL_DIRTY)) != 0)
00411 goto err;
00412 meta = NULL;
00413 if ((ret = CDB_memp_fput(dbp->mpf, root, DB_MPOOL_DIRTY)) != 0)
00414 goto err;
00415 root = NULL;
00416
00417
00418
00419
00420
00421
00422
00423
00424 if ((ret = CDB_memp_fsync(dbp->mpf)) == DB_INCOMPLETE) {
00425 CDB___db_err(dbp->dbenv, "Metapage flush failed");
00426 ret = EINVAL;
00427 }
00428 DB_TEST_RECOVERY(dbp, DB_TEST_POSTSYNC, ret, name);
00429
00430 done:
00431
00432
00433
00434
00435
00436 t->bt_lpgno = PGNO_INVALID;
00437
00438 err:
00439 DB_TEST_RECOVERY_LABEL
00440
00441 if (meta != NULL)
00442 if ((t_ret = CDB_memp_fput(dbp->mpf, meta, 0)) != 0 &&
00443 ret == 0)
00444 ret = t_ret;
00445 if (root != NULL)
00446 if ((t_ret = CDB_memp_fput(dbp->mpf, root, 0)) != 0 &&
00447 ret == 0)
00448 ret = t_ret;
00449
00450
00451 (void)__LPUT(dbc, metalock);
00452
00453 if ((t_ret = dbc->c_close(dbc)) != 0 && ret == 0)
00454 ret = t_ret;
00455 return (ret);
00456 }