/* Do not edit: automatically built by dist/distrib. */
#include <sys/queue.h>
#include <sys/shqueue.h>

#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#include "db_int.h"
#include "db_page.h"
#include "../log/log.h"
#include "bam.h"
#include "../common/db_dispatch.h"
#include "../common/extern.h"

/*
 * __bam_pg_alloc_recover --
 *	Recovery function for pg_alloc.
 *
 * PUBLIC: int __bam_pg_alloc_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_pg_alloc_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_pg_alloc_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_pg_alloc_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_pg_alloc_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __bam_pg_free_recover --
 *	Recovery function for pg_free.
 *
 * PUBLIC: int __bam_pg_free_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_pg_free_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_pg_free_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_pg_free_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_pg_free_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __bam_split_recover --
 *	Recovery function for split.
 *
 * PUBLIC: int __bam_split_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_split_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_split_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_split_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_split_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __bam_rsplit_recover --
 *	Recovery function for rsplit.
 *
 * PUBLIC: int __bam_rsplit_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_rsplit_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_rsplit_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_rsplit_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_rsplit_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __bam_adj_recover --
 *	Recovery function for adj.
 *
 * PUBLIC: int __bam_adj_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_adj_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_adj_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_adj_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_adj_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __bam_cadjust_recover --
 *	Recovery function for cadjust.
 *
 * PUBLIC: int __bam_cadjust_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_cadjust_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_cadjust_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_cadjust_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_cadjust_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __bam_cdel_recover --
 *	Recovery function for cdel.
 *
 * PUBLIC: int __bam_cdel_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_cdel_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_cdel_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_cdel_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_cdel_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}

/*
 * __bam_repl_recover --
 *	Recovery function for repl.
 *
 * PUBLIC: int __bam_repl_recover
 * PUBLIC:   __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
 */
int
__bam_repl_recover(logp, dbtp, lsnp, redo, info)
	DB_LOG *logp;
	DBT *dbtp;
	DB_LSN *lsnp;
	int redo;
	void *info;
{
	__bam_repl_args *argp;
	DB *file_dbp;
	DB_MPOOLFILE *mpf;
	PAGE *pagep;
	int cmp_n, cmp_p, modified, ret;

#ifdef DEBUG_RECOVER
	(void)__bam_repl_print(logp, dbtp, lsnp, redo, info);
#endif
	info = info;				/* XXX: Shut the compiler up. */

	if ((ret = __bam_repl_read(dbtp->data, &argp)) != 0)
		goto out;
	if (__db_fileid_to_db(logp, &file_dbp, argp->fileid)) {
		ret = 0;
		goto out;
	}
	if (file_dbp == NULL)
		goto out;

	F_SET(file_dbp, DB_AM_RECOVER);
	mpf = file_dbp->mpf;

	if ((ret = memp_fget(mpf, &argp->pgno, 0, &pagep)) != 0)
		if (redo) {
			if ((ret = memp_fget(mpf,
			    &argp->pgno, DB_MPOOL_CREATE, &pagep)) != 0)
				goto out;
		} else {
			*lsnp = *argp->prev_lsn;
			ret = 0;
			goto out;
		}

	modified = 0;
	cmp_n = log_compare(lsnp, &LSN(pagep));

	/*
	 * Use this when there is something like "pagelsn" in the argp
	 * structure.  Sometimes, you might need to compare meta-data
	 * lsn's instead.
	 *
	 * cmp_p = log_compare(&LSN(pagep), argp->pagelsn);
	 */
	if (cmp_p == 0 && redo) {
		/* Need to redo update described. */
		modified = 1;
	} else if (cmp_n == 0 && !redo) {
		/* Need to undo update described. */
		modified = 1;
	}
	if (ret = memp_fput(mpf, pagep, modified ? DB_MPOOL_DIRTY : 0))
		goto out;

	*lsnp = *argp->prev_lsn;

out:	if (argp != NULL)
		free (argp);
	F_CLR(file_dbp, DB_AM_RECOVER);
	return (ret);
}
