db_vrfyutil.c 18.9 KB
Newer Older
unknown's avatar
unknown committed
1 2 3
/*-
 * See the file LICENSE for redistribution information.
 *
unknown's avatar
unknown committed
4
 * Copyright (c) 2000-2002
unknown's avatar
unknown committed
5 6
 *	Sleepycat Software.  All rights reserved.
 *
unknown's avatar
unknown committed
7
 * $Id: db_vrfyutil.c,v 11.29 2002/08/08 03:57:50 bostic Exp $
unknown's avatar
unknown committed
8 9 10 11 12
 */

#include "db_config.h"

#ifndef lint
unknown's avatar
unknown committed
13
static const char revid[] = "$Id: db_vrfyutil.c,v 11.29 2002/08/08 03:57:50 bostic Exp $";
unknown's avatar
unknown committed
14 15 16 17 18 19 20 21 22
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#include <string.h>
#endif

#include "db_int.h"
unknown's avatar
unknown committed
23 24 25
#include "dbinc/db_page.h"
#include "dbinc/db_verify.h"
#include "dbinc/db_am.h"
unknown's avatar
unknown committed
26

unknown's avatar
unknown committed
27
static int __db_vrfy_pageinfo_create __P((DB_ENV *, VRFY_PAGEINFO **));
unknown's avatar
unknown committed
28 29 30 31 32 33 34 35 36 37
static int __db_vrfy_pgset_iinc __P((DB *, db_pgno_t, int));

/*
 * __db_vrfy_dbinfo_create --
 *	Allocate and initialize a VRFY_DBINFO structure.
 *
 * PUBLIC: int __db_vrfy_dbinfo_create
 * PUBLIC:     __P((DB_ENV *, u_int32_t, VRFY_DBINFO **));
 */
int
unknown's avatar
unknown committed
38
__db_vrfy_dbinfo_create(dbenv, pgsize, vdpp)
unknown's avatar
unknown committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	DB_ENV *dbenv;
	u_int32_t pgsize;
	VRFY_DBINFO **vdpp;
{
	DB *cdbp, *pgdbp, *pgset;
	VRFY_DBINFO *vdp;
	int ret;

	vdp = NULL;
	cdbp = pgdbp = pgset = NULL;

	if ((ret = __os_calloc(NULL,
	    1, sizeof(VRFY_DBINFO), (void **)&vdp)) != 0)
		goto err;

	if ((ret = db_create(&cdbp, dbenv, 0)) != 0)
		goto err;

unknown's avatar
unknown committed
57
	if ((ret = cdbp->set_flags(cdbp, DB_DUP)) != 0)
unknown's avatar
unknown committed
58 59 60 61 62 63
		goto err;

	if ((ret = cdbp->set_pagesize(cdbp, pgsize)) != 0)
		goto err;

	if ((ret =
unknown's avatar
unknown committed
64
	    cdbp->open(cdbp, NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0600)) != 0)
unknown's avatar
unknown committed
65 66 67 68 69 70 71 72
		goto err;

	if ((ret = db_create(&pgdbp, dbenv, 0)) != 0)
		goto err;

	if ((ret = pgdbp->set_pagesize(pgdbp, pgsize)) != 0)
		goto err;

unknown's avatar
unknown committed
73 74
	if ((ret = pgdbp->open(pgdbp,
	    NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0600)) != 0)
unknown's avatar
unknown committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
		goto err;

	if ((ret = __db_vrfy_pgset(dbenv, pgsize, &pgset)) != 0)
		goto err;

	LIST_INIT(&vdp->subdbs);
	LIST_INIT(&vdp->activepips);

	vdp->cdbp = cdbp;
	vdp->pgdbp = pgdbp;
	vdp->pgset = pgset;
	*vdpp = vdp;
	return (0);

err:	if (cdbp != NULL)
		(void)cdbp->close(cdbp, 0);
	if (pgdbp != NULL)
		(void)pgdbp->close(pgdbp, 0);
	if (vdp != NULL)
unknown's avatar
unknown committed
94
		__os_free(dbenv, vdp);
unknown's avatar
unknown committed
95 96 97 98 99 100 101 102
	return (ret);
}

/*
 * __db_vrfy_dbinfo_destroy --
 *	Destructor for VRFY_DBINFO.  Destroys VRFY_PAGEINFOs and deallocates
 *	structure.
 *
unknown's avatar
unknown committed
103
 * PUBLIC: int __db_vrfy_dbinfo_destroy __P((DB_ENV *, VRFY_DBINFO *));
unknown's avatar
unknown committed
104 105
 */
int
unknown's avatar
unknown committed
106 107
__db_vrfy_dbinfo_destroy(dbenv, vdp)
	DB_ENV *dbenv;
unknown's avatar
unknown committed
108 109 110 111 112 113 114 115 116
	VRFY_DBINFO *vdp;
{
	VRFY_CHILDINFO *c, *d;
	int t_ret, ret;

	ret = 0;

	for (c = LIST_FIRST(&vdp->subdbs); c != NULL; c = d) {
		d = LIST_NEXT(c, links);
unknown's avatar
unknown committed
117
		__os_free(NULL, c);
unknown's avatar
unknown committed
118 119 120 121 122 123 124 125 126 127 128 129 130
	}

	if ((t_ret = vdp->pgdbp->close(vdp->pgdbp, 0)) != 0)
		ret = t_ret;

	if ((t_ret = vdp->cdbp->close(vdp->cdbp, 0)) != 0 && ret == 0)
		ret = t_ret;

	if ((t_ret = vdp->pgset->close(vdp->pgset, 0)) != 0 && ret == 0)
		ret = t_ret;

	DB_ASSERT(LIST_FIRST(&vdp->activepips) == NULL);

unknown's avatar
unknown committed
131
	__os_free(dbenv, vdp);
unknown's avatar
unknown committed
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
	return (ret);
}

/*
 * __db_vrfy_getpageinfo --
 *	Get a PAGEINFO structure for a given page, creating it if necessary.
 *
 * PUBLIC: int __db_vrfy_getpageinfo
 * PUBLIC:     __P((VRFY_DBINFO *, db_pgno_t, VRFY_PAGEINFO **));
 */
int
__db_vrfy_getpageinfo(vdp, pgno, pipp)
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
	VRFY_PAGEINFO **pipp;
{
	DBT key, data;
	DB *pgdbp;
	VRFY_PAGEINFO *pip;
	int ret;

	/*
	 * We want a page info struct.  There are three places to get it from,
	 * in decreasing order of preference:
	 *
	 * 1. vdp->activepips.  If it's already "checked out", we're
	 *	already using it, we return the same exact structure with a
	 *	bumped refcount.  This is necessary because this code is
	 *	replacing array accesses, and it's common for f() to make some
	 *	changes to a pip, and then call g() and h() which each make
	 *	changes to the same pip.  vdps are never shared between threads
	 *	(they're never returned to the application), so this is safe.
	 * 2. The pgdbp.  It's not in memory, but it's in the database, so
	 *	get it, give it a refcount of 1, and stick it on activepips.
	 * 3. malloc.  It doesn't exist yet;  create it, then stick it on
	 *	activepips.  We'll put it in the database when we putpageinfo
	 *	later.
	 */

	/* Case 1. */
	for (pip = LIST_FIRST(&vdp->activepips); pip != NULL;
	    pip = LIST_NEXT(pip, links))
		if (pip->pgno == pgno)
			/* Found it. */
			goto found;

	/* Case 2. */
	pgdbp = vdp->pgdbp;
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	F_SET(&data, DB_DBT_MALLOC);
	key.data = &pgno;
	key.size = sizeof(db_pgno_t);

	if ((ret = pgdbp->get(pgdbp, NULL, &key, &data, 0)) == 0) {
		/* Found it. */
		DB_ASSERT(data.size = sizeof(VRFY_PAGEINFO));
		pip = data.data;
		DB_ASSERT(pip->pi_refcount == 0);
		LIST_INSERT_HEAD(&vdp->activepips, pip, links);
		goto found;
	} else if (ret != DB_NOTFOUND)	/* Something nasty happened. */
		return (ret);

	/* Case 3 */
unknown's avatar
unknown committed
197
	if ((ret = __db_vrfy_pageinfo_create(pgdbp->dbenv, &pip)) != 0)
unknown's avatar
unknown committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		return (ret);

	LIST_INSERT_HEAD(&vdp->activepips, pip, links);
found:	pip->pi_refcount++;

	*pipp = pip;

	DB_ASSERT(pip->pi_refcount > 0);
	return (0);
}

/*
 * __db_vrfy_putpageinfo --
 *	Put back a VRFY_PAGEINFO that we're done with.
 *
unknown's avatar
unknown committed
213 214
 * PUBLIC: int __db_vrfy_putpageinfo __P((DB_ENV *,
 * PUBLIC:     VRFY_DBINFO *, VRFY_PAGEINFO *));
unknown's avatar
unknown committed
215 216
 */
int
unknown's avatar
unknown committed
217 218
__db_vrfy_putpageinfo(dbenv, vdp, pip)
	DB_ENV *dbenv;
unknown's avatar
unknown committed
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
	VRFY_DBINFO *vdp;
	VRFY_PAGEINFO *pip;
{
	DBT key, data;
	DB *pgdbp;
	VRFY_PAGEINFO *p;
	int ret;
#ifdef DIAGNOSTIC
	int found;

	found = 0;
#endif

	if (--pip->pi_refcount > 0)
		return (0);

	pgdbp = vdp->pgdbp;
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.data = &pip->pgno;
	key.size = sizeof(db_pgno_t);
	data.data = pip;
	data.size = sizeof(VRFY_PAGEINFO);

	if ((ret = pgdbp->put(pgdbp, NULL, &key, &data, 0)) != 0)
		return (ret);

	for (p = LIST_FIRST(&vdp->activepips); p != NULL;
	    p = LIST_NEXT(p, links))
		if (p == pip) {
#ifdef DIAGNOSTIC
			found++;
#endif
			DB_ASSERT(p->pi_refcount == 0);
			LIST_REMOVE(p, links);
			break;
		}
#ifdef DIAGNOSTIC
	DB_ASSERT(found == 1);
#endif

	DB_ASSERT(pip->pi_refcount == 0);
unknown's avatar
unknown committed
262
	__os_ufree(dbenv, pip);
unknown's avatar
unknown committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	return (0);
}

/*
 * __db_vrfy_pgset --
 *	Create a temporary database for the storing of sets of page numbers.
 *	(A mapping from page number to int, used by the *_meta2pgset functions,
 *	as well as for keeping track of which pages the verifier has seen.)
 *
 * PUBLIC: int __db_vrfy_pgset __P((DB_ENV *, u_int32_t, DB **));
 */
int
__db_vrfy_pgset(dbenv, pgsize, dbpp)
	DB_ENV *dbenv;
	u_int32_t pgsize;
	DB **dbpp;
{
	DB *dbp;
	int ret;

	if ((ret = db_create(&dbp, dbenv, 0)) != 0)
		return (ret);
	if ((ret = dbp->set_pagesize(dbp, pgsize)) != 0)
		goto err;
unknown's avatar
unknown committed
287 288
	if ((ret = dbp->open(dbp,
	    NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0600)) == 0)
unknown's avatar
unknown committed
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
		*dbpp = dbp;
	else
err:		(void)dbp->close(dbp, 0);

	return (ret);
}

/*
 * __db_vrfy_pgset_get --
 *	Get the value associated in a page set with a given pgno.  Return
 *	a 0 value (and succeed) if we've never heard of this page.
 *
 * PUBLIC: int __db_vrfy_pgset_get __P((DB *, db_pgno_t, int *));
 */
int
__db_vrfy_pgset_get(dbp, pgno, valp)
	DB *dbp;
	db_pgno_t pgno;
	int *valp;
{
	DBT key, data;
	int ret, val;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.data = &pgno;
	key.size = sizeof(db_pgno_t);
	data.data = &val;
	data.ulen = sizeof(int);
	F_SET(&data, DB_DBT_USERMEM);

	if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) {
		DB_ASSERT(data.size = sizeof(int));
		memcpy(&val, data.data, sizeof(int));
	} else if (ret == DB_NOTFOUND)
		val = 0;
	else
		return (ret);

	*valp = val;
	return (0);
}

/*
 * __db_vrfy_pgset_inc --
 *	Increment the value associated with a pgno by 1.
 *
 * PUBLIC: int __db_vrfy_pgset_inc __P((DB *, db_pgno_t));
 */
int
__db_vrfy_pgset_inc(dbp, pgno)
	DB *dbp;
	db_pgno_t pgno;
{

	return (__db_vrfy_pgset_iinc(dbp, pgno, 1));
}

/*
 * __db_vrfy_pgset_dec --
 *	Increment the value associated with a pgno by 1.
 *
 * PUBLIC: int __db_vrfy_pgset_dec __P((DB *, db_pgno_t));
 */
int
__db_vrfy_pgset_dec(dbp, pgno)
	DB *dbp;
	db_pgno_t pgno;
{

	return (__db_vrfy_pgset_iinc(dbp, pgno, -1));
}

/*
 * __db_vrfy_pgset_iinc --
 *	Increment the value associated with a pgno by i.
 *
 */
static int
__db_vrfy_pgset_iinc(dbp, pgno, i)
	DB *dbp;
	db_pgno_t pgno;
	int i;
{
	DBT key, data;
	int ret;
	int val;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	val = 0;

	key.data = &pgno;
	key.size = sizeof(db_pgno_t);
	data.data = &val;
	data.ulen = sizeof(int);
	F_SET(&data, DB_DBT_USERMEM);

	if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0) {
unknown's avatar
unknown committed
390
		DB_ASSERT(data.size == sizeof(int));
unknown's avatar
unknown committed
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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		memcpy(&val, data.data, sizeof(int));
	} else if (ret != DB_NOTFOUND)
		return (ret);

	data.size = sizeof(int);
	val += i;

	return (dbp->put(dbp, NULL, &key, &data, 0));
}

/*
 * __db_vrfy_pgset_next --
 *	Given a cursor open in a pgset database, get the next page in the
 *	set.
 *
 * PUBLIC: int __db_vrfy_pgset_next __P((DBC *, db_pgno_t *));
 */
int
__db_vrfy_pgset_next(dbc, pgnop)
	DBC *dbc;
	db_pgno_t *pgnop;
{
	DBT key, data;
	db_pgno_t pgno;
	int ret;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));
	/* We don't care about the data, just the keys. */
	F_SET(&data, DB_DBT_USERMEM | DB_DBT_PARTIAL);
	F_SET(&key, DB_DBT_USERMEM);
	key.data = &pgno;
	key.ulen = sizeof(db_pgno_t);

	if ((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) != 0)
		return (ret);

	DB_ASSERT(key.size == sizeof(db_pgno_t));
	*pgnop = pgno;

	return (0);
}

/*
 * __db_vrfy_childcursor --
 *	Create a cursor to walk the child list with.  Returns with a nonzero
 *	final argument if the specified page has no children.
 *
 * PUBLIC: int __db_vrfy_childcursor __P((VRFY_DBINFO *, DBC **));
 */
int
__db_vrfy_childcursor(vdp, dbcp)
	VRFY_DBINFO *vdp;
	DBC **dbcp;
{
	DB *cdbp;
	DBC *dbc;
	int ret;

	cdbp = vdp->cdbp;

	if ((ret = cdbp->cursor(cdbp, NULL, &dbc, 0)) == 0)
		*dbcp = dbc;

	return (ret);
}

/*
 * __db_vrfy_childput --
 *	Add a child structure to the set for a given page.
 *
 * PUBLIC: int __db_vrfy_childput
 * PUBLIC:     __P((VRFY_DBINFO *, db_pgno_t, VRFY_CHILDINFO *));
 */
int
__db_vrfy_childput(vdp, pgno, cip)
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
	VRFY_CHILDINFO *cip;
{
	DB *cdbp;
unknown's avatar
unknown committed
472 473 474
	DBC *cc;
	DBT key, data;
	VRFY_CHILDINFO *oldcip;
unknown's avatar
unknown committed
475 476 477 478 479 480 481 482 483
	int ret;

	cdbp = vdp->cdbp;
	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.data = &pgno;
	key.size = sizeof(db_pgno_t);

unknown's avatar
unknown committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
	/*
	 * We want to avoid adding multiple entries for a single child page;
	 * we only need to verify each child once, even if a child (such
	 * as an overflow key) is multiply referenced.
	 *
	 * However, we also need to make sure that when walking the list
	 * of children, we encounter them in the order they're referenced
	 * on a page.  (This permits us, for example, to verify the
	 * prev_pgno/next_pgno chain of Btree leaf pages.)
	 *
	 * Check the child database to make sure that this page isn't
	 * already a child of the specified page number.  If it's not,
	 * put it at the end of the duplicate set.
	 */
	if ((ret = __db_vrfy_childcursor(vdp, &cc)) != 0)
		return (ret);
	for (ret = __db_vrfy_ccset(cc, pgno, &oldcip); ret == 0;
	    ret = __db_vrfy_ccnext(cc, &oldcip))
		if (oldcip->pgno == cip->pgno) {
			/*
			 * Found a matching child.  Return without
			 * putting it again.
			 */
			if ((ret = __db_vrfy_ccclose(cc)) != 0)
				return (ret);
			return (0);
		}
	if (ret != DB_NOTFOUND) {
		(void)__db_vrfy_ccclose(cc);
		return (ret);
	}
	if ((ret = __db_vrfy_ccclose(cc)) != 0)
		return (ret);

unknown's avatar
unknown committed
518 519 520
	data.data = cip;
	data.size = sizeof(VRFY_CHILDINFO);

unknown's avatar
unknown committed
521
	return (cdbp->put(cdbp, NULL, &key, &data, 0));
unknown's avatar
unknown committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
}

/*
 * __db_vrfy_ccset --
 *	Sets a cursor created with __db_vrfy_childcursor to the first
 *	child of the given pgno, and returns it in the third arg.
 *
 * PUBLIC: int __db_vrfy_ccset __P((DBC *, db_pgno_t, VRFY_CHILDINFO **));
 */
int
__db_vrfy_ccset(dbc, pgno, cipp)
	DBC *dbc;
	db_pgno_t pgno;
	VRFY_CHILDINFO **cipp;
{
	DBT key, data;
	int ret;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.data = &pgno;
	key.size = sizeof(db_pgno_t);

	if ((ret = dbc->c_get(dbc, &key, &data, DB_SET)) != 0)
		return (ret);

	DB_ASSERT(data.size == sizeof(VRFY_CHILDINFO));
	*cipp = (VRFY_CHILDINFO *)data.data;

	return (0);
}

/*
 * __db_vrfy_ccnext --
 *	Gets the next child of the given cursor created with
 *	__db_vrfy_childcursor, and returns it in the memory provided in the
 *	second arg.
 *
 * PUBLIC: int __db_vrfy_ccnext __P((DBC *, VRFY_CHILDINFO **));
 */
int
__db_vrfy_ccnext(dbc, cipp)
	DBC *dbc;
	VRFY_CHILDINFO **cipp;
{
	DBT key, data;
	int ret;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	if ((ret = dbc->c_get(dbc, &key, &data, DB_NEXT_DUP)) != 0)
		return (ret);

	DB_ASSERT(data.size == sizeof(VRFY_CHILDINFO));
	*cipp = (VRFY_CHILDINFO *)data.data;

	return (0);
}

/*
 * __db_vrfy_ccclose --
 *	Closes the cursor created with __db_vrfy_childcursor.
 *
 *	This doesn't actually do anything interesting now, but it's
 *	not inconceivable that we might change the internal database usage
 *	and keep the interfaces the same, and a function call here or there
 *	seldom hurts anyone.
 *
 * PUBLIC: int __db_vrfy_ccclose __P((DBC *));
 */
int
__db_vrfy_ccclose(dbc)
	DBC *dbc;
{

	return (dbc->c_close(dbc));
}

/*
 * __db_vrfy_pageinfo_create --
 *	Constructor for VRFY_PAGEINFO;  allocates and initializes.
 */
unknown's avatar
unknown committed
606 607 608
static int
__db_vrfy_pageinfo_create(dbenv, pgipp)
	DB_ENV *dbenv;
unknown's avatar
unknown committed
609 610 611 612 613
	VRFY_PAGEINFO **pgipp;
{
	VRFY_PAGEINFO *pgip;
	int ret;

unknown's avatar
unknown committed
614 615 616 617 618 619 620 621 622
	/*
	 * pageinfo structs are sometimes allocated here and sometimes
	 * allocated by fetching them from a database with DB_DBT_MALLOC.
	 * There's no easy way for the destructor to tell which was
	 * used, and so we always allocate with __os_umalloc so we can free
	 * with __os_ufree.
	 */
	if ((ret = __os_umalloc(dbenv,
	    sizeof(VRFY_PAGEINFO), (void **)&pgip)) != 0)
unknown's avatar
unknown committed
623
		return (ret);
unknown's avatar
unknown committed
624
	memset(pgip, 0, sizeof(VRFY_PAGEINFO));
unknown's avatar
unknown committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

	DB_ASSERT(pgip->pi_refcount == 0);

	*pgipp = pgip;
	return (0);
}

/*
 * __db_salvage_init --
 *	Set up salvager database.
 *
 * PUBLIC: int  __db_salvage_init __P((VRFY_DBINFO *));
 */
int
__db_salvage_init(vdp)
	VRFY_DBINFO *vdp;
{
	DB *dbp;
	int ret;

	if ((ret = db_create(&dbp, NULL, 0)) != 0)
		return (ret);

	if ((ret = dbp->set_pagesize(dbp, 1024)) != 0)
		goto err;

unknown's avatar
unknown committed
651 652
	if ((ret = dbp->open(dbp,
	    NULL, NULL, NULL, DB_BTREE, DB_CREATE, 0)) != 0)
unknown's avatar
unknown committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
		goto err;

	vdp->salvage_pages = dbp;
	return (0);

err:	(void)dbp->close(dbp, 0);
	return (ret);
}

/*
 * __db_salvage_destroy --
 *	Close salvager database.
 * PUBLIC: void  __db_salvage_destroy __P((VRFY_DBINFO *));
 */
void
__db_salvage_destroy(vdp)
	VRFY_DBINFO *vdp;
{
	(void)vdp->salvage_pages->close(vdp->salvage_pages, 0);
}

/*
 * __db_salvage_getnext --
 *	Get the next (first) unprinted page in the database of pages we need to
 *	print still.  Delete entries for any already-printed pages we encounter
 *	in this search, as well as the page we're returning.
 *
 * PUBLIC: int __db_salvage_getnext
 * PUBLIC:     __P((VRFY_DBINFO *, db_pgno_t *, u_int32_t *));
 */
int
__db_salvage_getnext(vdp, pgnop, pgtypep)
	VRFY_DBINFO *vdp;
	db_pgno_t *pgnop;
	u_int32_t *pgtypep;
{
	DB *dbp;
	DBC *dbc;
	DBT key, data;
	int ret;
	u_int32_t pgtype;

	dbp = vdp->salvage_pages;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	if ((ret = dbp->cursor(dbp, NULL, &dbc, 0)) != 0)
		return (ret);

	while ((ret = dbc->c_get(dbc, &key, &data, DB_NEXT)) == 0) {
		DB_ASSERT(data.size == sizeof(u_int32_t));
		memcpy(&pgtype, data.data, sizeof(pgtype));

		if ((ret = dbc->c_del(dbc, 0)) != 0)
			goto err;
		if (pgtype != SALVAGE_IGNORE)
			goto found;
	}

	/* No more entries--ret probably equals DB_NOTFOUND. */

	if (0) {
found:		DB_ASSERT(key.size == sizeof(db_pgno_t));
		DB_ASSERT(data.size == sizeof(u_int32_t));

		*pgnop = *(db_pgno_t *)key.data;
		*pgtypep = *(u_int32_t *)data.data;
	}

err:	(void)dbc->c_close(dbc);
	return (ret);
}

/*
 * __db_salvage_isdone --
 *	Return whether or not the given pgno is already marked
 *	SALVAGE_IGNORE (meaning that we don't need to print it again).
 *
 *	Returns DB_KEYEXIST if it is marked, 0 if not, or another error on
 *	error.
 *
 * PUBLIC: int __db_salvage_isdone __P((VRFY_DBINFO *, db_pgno_t));
 */
int
__db_salvage_isdone(vdp, pgno)
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
{
	DBT key, data;
	DB *dbp;
	int ret;
	u_int32_t currtype;

	dbp = vdp->salvage_pages;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	currtype = SALVAGE_INVALID;
	data.data = &currtype;
	data.ulen = sizeof(u_int32_t);
	data.flags = DB_DBT_USERMEM;

	key.data = &pgno;
	key.size = sizeof(db_pgno_t);

	/*
	 * Put an entry for this page, with pgno as key and type as data,
	 * unless it's already there and is marked done.
	 * If it's there and is marked anything else, that's fine--we
	 * want to mark it done.
	 */
	ret = dbp->get(dbp, NULL, &key, &data, 0);
	if (ret == 0) {
		/*
		 * The key's already here.  Check and see if it's already
		 * marked done.  If it is, return DB_KEYEXIST.  If it's not,
		 * return 0.
		 */
		if (currtype == SALVAGE_IGNORE)
			return (DB_KEYEXIST);
		else
			return (0);
	} else if (ret != DB_NOTFOUND)
		return (ret);

	/* The pgno is not yet marked anything; return 0. */
	return (0);
}

/*
 * __db_salvage_markdone --
 *	Mark as done a given page.
 *
 * PUBLIC: int __db_salvage_markdone __P((VRFY_DBINFO *, db_pgno_t));
 */
int
__db_salvage_markdone(vdp, pgno)
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
{
	DBT key, data;
	DB *dbp;
	int pgtype, ret;
	u_int32_t currtype;

	pgtype = SALVAGE_IGNORE;
	dbp = vdp->salvage_pages;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	currtype = SALVAGE_INVALID;
	data.data = &currtype;
	data.ulen = sizeof(u_int32_t);
	data.flags = DB_DBT_USERMEM;

	key.data = &pgno;
	key.size = sizeof(db_pgno_t);

	/*
	 * Put an entry for this page, with pgno as key and type as data,
	 * unless it's already there and is marked done.
	 * If it's there and is marked anything else, that's fine--we
	 * want to mark it done, but db_salvage_isdone only lets
	 * us know if it's marked IGNORE.
	 *
	 * We don't want to return DB_KEYEXIST, though;  this will
	 * likely get passed up all the way and make no sense to the
	 * application.  Instead, use DB_VERIFY_BAD to indicate that
	 * we've seen this page already--it probably indicates a
	 * multiply-linked page.
	 */
	if ((ret = __db_salvage_isdone(vdp, pgno)) != 0)
		return (ret == DB_KEYEXIST ? DB_VERIFY_BAD : ret);

	data.size = sizeof(u_int32_t);
	data.data = &pgtype;

	return (dbp->put(dbp, NULL, &key, &data, 0));
}

/*
 * __db_salvage_markneeded --
 *	If it has not yet been printed, make note of the fact that a page
 *	must be dealt with later.
 *
 * PUBLIC: int __db_salvage_markneeded
 * PUBLIC:     __P((VRFY_DBINFO *, db_pgno_t, u_int32_t));
 */
int
__db_salvage_markneeded(vdp, pgno, pgtype)
	VRFY_DBINFO *vdp;
	db_pgno_t pgno;
	u_int32_t pgtype;
{
	DB *dbp;
	DBT key, data;
	int ret;

	dbp = vdp->salvage_pages;

	memset(&key, 0, sizeof(DBT));
	memset(&data, 0, sizeof(DBT));

	key.data = &pgno;
	key.size = sizeof(db_pgno_t);

	data.data = &pgtype;
	data.size = sizeof(u_int32_t);

	/*
	 * Put an entry for this page, with pgno as key and type as data,
	 * unless it's already there, in which case it's presumably
	 * already been marked done.
	 */
	ret = dbp->put(dbp, NULL, &key, &data, DB_NOOVERWRITE);
	return (ret == DB_KEYEXIST ? 0 : ret);
}