xref: /illumos-gate/usr/src/cmd/sendmail/db/db/db_apprec.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*-
2*7c478bd9Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
5*7c478bd9Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*7c478bd9Sstevel@tonic-gate  */
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate #include "config.h"
9*7c478bd9Sstevel@tonic-gate 
10*7c478bd9Sstevel@tonic-gate #ifndef lint
11*7c478bd9Sstevel@tonic-gate static const char copyright[] =
12*7c478bd9Sstevel@tonic-gate "@(#) Copyright (c) 1996, 1997, 1998\n\
13*7c478bd9Sstevel@tonic-gate 	Sleepycat Software Inc.  All rights reserved.\n";
14*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)db_apprec.c	10.33 (Sleepycat) 10/5/98";
15*7c478bd9Sstevel@tonic-gate #endif
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
18*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
19*7c478bd9Sstevel@tonic-gate 
20*7c478bd9Sstevel@tonic-gate #include <errno.h>
21*7c478bd9Sstevel@tonic-gate #include <string.h>
22*7c478bd9Sstevel@tonic-gate #include <time.h>
23*7c478bd9Sstevel@tonic-gate #endif
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate #include "db_int.h"
26*7c478bd9Sstevel@tonic-gate #include "shqueue.h"
27*7c478bd9Sstevel@tonic-gate #include "db_page.h"
28*7c478bd9Sstevel@tonic-gate #include "db_dispatch.h"
29*7c478bd9Sstevel@tonic-gate #include "db_am.h"
30*7c478bd9Sstevel@tonic-gate #include "log.h"
31*7c478bd9Sstevel@tonic-gate #include "txn.h"
32*7c478bd9Sstevel@tonic-gate #include "common_ext.h"
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * __db_apprec --
36*7c478bd9Sstevel@tonic-gate  *	Perform recovery.
37*7c478bd9Sstevel@tonic-gate  *
38*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_apprec __P((DB_ENV *, u_int32_t));
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate int
__db_apprec(dbenv,flags)41*7c478bd9Sstevel@tonic-gate __db_apprec(dbenv, flags)
42*7c478bd9Sstevel@tonic-gate 	DB_ENV *dbenv;
43*7c478bd9Sstevel@tonic-gate 	u_int32_t flags;
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	DBT data;
46*7c478bd9Sstevel@tonic-gate 	DB_LOG *lp;
47*7c478bd9Sstevel@tonic-gate 	DB_LSN ckp_lsn, first_lsn, lsn, open_lsn;
48*7c478bd9Sstevel@tonic-gate 	__txn_ckp_args *ckp_args;
49*7c478bd9Sstevel@tonic-gate 	time_t now;
50*7c478bd9Sstevel@tonic-gate 	u_int32_t is_thread;
51*7c478bd9Sstevel@tonic-gate 	int ret;
52*7c478bd9Sstevel@tonic-gate 	void *txninfo;
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	lp = dbenv->lg_info;
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 	/* Initialize the transaction list. */
57*7c478bd9Sstevel@tonic-gate 	if ((ret = __db_txnlist_init(&txninfo)) != 0)
58*7c478bd9Sstevel@tonic-gate 		return (ret);
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	/*
61*7c478bd9Sstevel@tonic-gate 	 * Save the state of the thread flag -- we don't need it on at the
62*7c478bd9Sstevel@tonic-gate 	 * moment because we're single-threaded until recovery is complete.
63*7c478bd9Sstevel@tonic-gate 	 */
64*7c478bd9Sstevel@tonic-gate 	is_thread = F_ISSET(lp, DB_AM_THREAD);
65*7c478bd9Sstevel@tonic-gate 	F_CLR(lp, DB_AM_THREAD);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	/*
68*7c478bd9Sstevel@tonic-gate 	 * Recovery is done in three passes:
69*7c478bd9Sstevel@tonic-gate 	 * Pass #0:
70*7c478bd9Sstevel@tonic-gate 	 *	We need to find the position from which we will open files
71*7c478bd9Sstevel@tonic-gate 	 *	We need to open files beginning with the last to next
72*7c478bd9Sstevel@tonic-gate 	 *	checkpoint because we might have crashed after writing the
73*7c478bd9Sstevel@tonic-gate 	 * 	last checkpoint record, but before having written out all
74*7c478bd9Sstevel@tonic-gate 	 *	the open file information.
75*7c478bd9Sstevel@tonic-gate 	 * Pass #1:
76*7c478bd9Sstevel@tonic-gate 	 *	Read forward through the log from the second to last checkpoint
77*7c478bd9Sstevel@tonic-gate 	 *	opening and closing files so that at the end of the log we have
78*7c478bd9Sstevel@tonic-gate 	 *	the "current" set of files open.
79*7c478bd9Sstevel@tonic-gate 	 * Pass #2:
80*7c478bd9Sstevel@tonic-gate 	 *	Read backward through the log undoing any uncompleted TXNs.
81*7c478bd9Sstevel@tonic-gate 	 *	If doing catastrophic recovery, we read to the beginning of
82*7c478bd9Sstevel@tonic-gate 	 *	the log, otherwise, to the most recent checkpoint that occurs
83*7c478bd9Sstevel@tonic-gate 	 *	before the most recent checkpoint LSN, which is returned by
84*7c478bd9Sstevel@tonic-gate 	 *	__log_findckp().  During this pass, checkpoint file information
85*7c478bd9Sstevel@tonic-gate 	 *	is ignored, and file openings and closings are undone.
86*7c478bd9Sstevel@tonic-gate 	 * Pass #3:
87*7c478bd9Sstevel@tonic-gate 	 *	Read forward through the log from the LSN found in pass #2,
88*7c478bd9Sstevel@tonic-gate 	 *	redoing any committed TXNs.  During this pass, checkpoint
89*7c478bd9Sstevel@tonic-gate 	 *	file information is ignored, and file openings and closings
90*7c478bd9Sstevel@tonic-gate 	 *	are redone.
91*7c478bd9Sstevel@tonic-gate 	 */
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	/*
94*7c478bd9Sstevel@tonic-gate 	 * Find the second to last checkpoint in the log.  This is the point
95*7c478bd9Sstevel@tonic-gate 	 * from which we want to begin pass #1 (the TXN_OPENFILES pass).
96*7c478bd9Sstevel@tonic-gate 	 */
97*7c478bd9Sstevel@tonic-gate 	memset(&data, 0, sizeof(data));
98*7c478bd9Sstevel@tonic-gate 	ckp_args = NULL;
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if ((ret = log_get(lp, &ckp_lsn, &data, DB_CHECKPOINT)) != 0) {
101*7c478bd9Sstevel@tonic-gate 		/*
102*7c478bd9Sstevel@tonic-gate 		 * If we don't find a checkpoint, start from the beginning.
103*7c478bd9Sstevel@tonic-gate 		 * If that fails, we're done.  Note, we do not require that
104*7c478bd9Sstevel@tonic-gate 		 * there be log records if we're performing recovery.
105*7c478bd9Sstevel@tonic-gate 		 */
106*7c478bd9Sstevel@tonic-gate first:		if ((ret = log_get(lp, &ckp_lsn, &data, DB_FIRST)) != 0) {
107*7c478bd9Sstevel@tonic-gate 			if (ret == DB_NOTFOUND)
108*7c478bd9Sstevel@tonic-gate 				ret = 0;
109*7c478bd9Sstevel@tonic-gate 			else
110*7c478bd9Sstevel@tonic-gate 				__db_err(dbenv, "First log record not found");
111*7c478bd9Sstevel@tonic-gate 			goto out;
112*7c478bd9Sstevel@tonic-gate 		}
113*7c478bd9Sstevel@tonic-gate 		open_lsn = ckp_lsn;
114*7c478bd9Sstevel@tonic-gate 	} else if ((ret = __txn_ckp_read(data.data, &ckp_args)) != 0) {
115*7c478bd9Sstevel@tonic-gate 		__db_err(dbenv, "Invalid checkpoint record at [%ld][%ld]\n",
116*7c478bd9Sstevel@tonic-gate 		    (u_long)ckp_lsn.file, (u_long)ckp_lsn.offset);
117*7c478bd9Sstevel@tonic-gate 		goto out;
118*7c478bd9Sstevel@tonic-gate 	} else if (IS_ZERO_LSN(ckp_args->last_ckp) ||
119*7c478bd9Sstevel@tonic-gate 		(ret = log_get(lp, &ckp_args->last_ckp, &data, DB_SET)) != 0)
120*7c478bd9Sstevel@tonic-gate 		goto first;
121*7c478bd9Sstevel@tonic-gate 	else
122*7c478bd9Sstevel@tonic-gate 		open_lsn = ckp_args->last_ckp;
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	/*
125*7c478bd9Sstevel@tonic-gate 	 * Now, ckp_lsn is either the lsn of the last checkpoint or the lsn
126*7c478bd9Sstevel@tonic-gate 	 * of the first record in the log.  Open_lsn is the second to last
127*7c478bd9Sstevel@tonic-gate 	 * checkpoint or the beinning of the log; begin the TXN_OPENFILES
128*7c478bd9Sstevel@tonic-gate 	 * pass from that lsn, and proceed to the end of the log.
129*7c478bd9Sstevel@tonic-gate 	 */
130*7c478bd9Sstevel@tonic-gate 	lsn = open_lsn;
131*7c478bd9Sstevel@tonic-gate 	for (;;) {
132*7c478bd9Sstevel@tonic-gate 		if (dbenv->tx_recover != NULL)
133*7c478bd9Sstevel@tonic-gate 			ret = dbenv->tx_recover(lp,
134*7c478bd9Sstevel@tonic-gate 			    &data, &lsn, TXN_OPENFILES, txninfo);
135*7c478bd9Sstevel@tonic-gate 		else
136*7c478bd9Sstevel@tonic-gate 			ret = __db_dispatch(lp,
137*7c478bd9Sstevel@tonic-gate 			    &data, &lsn, TXN_OPENFILES, txninfo);
138*7c478bd9Sstevel@tonic-gate 		if (ret != 0 && ret != DB_TXN_CKP)
139*7c478bd9Sstevel@tonic-gate 			goto msgerr;
140*7c478bd9Sstevel@tonic-gate 		if ((ret = log_get(lp, &lsn, &data, DB_NEXT)) != 0) {
141*7c478bd9Sstevel@tonic-gate 			if (ret == DB_NOTFOUND)
142*7c478bd9Sstevel@tonic-gate 				break;
143*7c478bd9Sstevel@tonic-gate 			goto out;
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	/*
148*7c478bd9Sstevel@tonic-gate 	 * Pass #2.
149*7c478bd9Sstevel@tonic-gate 	 *
150*7c478bd9Sstevel@tonic-gate 	 * Before we can begin pass #2, backward roll phase, we determine how
151*7c478bd9Sstevel@tonic-gate 	 * far back in the log to recover.  If we are doing catastrophic
152*7c478bd9Sstevel@tonic-gate 	 * recovery, then we go as far back as we have files.  If we are
153*7c478bd9Sstevel@tonic-gate 	 * doing normal recovery, we go as back to the most recent checkpoint
154*7c478bd9Sstevel@tonic-gate 	 * that occurs before the most recent checkpoint LSN.
155*7c478bd9Sstevel@tonic-gate 	 */
156*7c478bd9Sstevel@tonic-gate 	if (LF_ISSET(DB_RECOVER_FATAL)) {
157*7c478bd9Sstevel@tonic-gate 		ZERO_LSN(first_lsn);
158*7c478bd9Sstevel@tonic-gate 	} else
159*7c478bd9Sstevel@tonic-gate 		if ((ret = __log_findckp(lp, &first_lsn)) == DB_NOTFOUND) {
160*7c478bd9Sstevel@tonic-gate 			/*
161*7c478bd9Sstevel@tonic-gate 			 * We don't require that log files exist if recovery
162*7c478bd9Sstevel@tonic-gate 			 * was specified.
163*7c478bd9Sstevel@tonic-gate 			 */
164*7c478bd9Sstevel@tonic-gate 			ret = 0;
165*7c478bd9Sstevel@tonic-gate 			goto out;
166*7c478bd9Sstevel@tonic-gate 		}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	if (dbenv->db_verbose)
169*7c478bd9Sstevel@tonic-gate 		__db_err(lp->dbenv, "Recovery starting from [%lu][%lu]",
170*7c478bd9Sstevel@tonic-gate 		    (u_long)first_lsn.file, (u_long)first_lsn.offset);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	for (ret = log_get(lp, &lsn, &data, DB_LAST);
173*7c478bd9Sstevel@tonic-gate 	    ret == 0 && log_compare(&lsn, &first_lsn) > 0;
174*7c478bd9Sstevel@tonic-gate 	    ret = log_get(lp, &lsn, &data, DB_PREV)) {
175*7c478bd9Sstevel@tonic-gate 		if (dbenv->tx_recover != NULL)
176*7c478bd9Sstevel@tonic-gate 			ret = dbenv->tx_recover(lp,
177*7c478bd9Sstevel@tonic-gate 			    &data, &lsn, TXN_BACKWARD_ROLL, txninfo);
178*7c478bd9Sstevel@tonic-gate 		else
179*7c478bd9Sstevel@tonic-gate 			ret = __db_dispatch(lp,
180*7c478bd9Sstevel@tonic-gate 			    &data, &lsn, TXN_BACKWARD_ROLL, txninfo);
181*7c478bd9Sstevel@tonic-gate 		if (ret != 0)
182*7c478bd9Sstevel@tonic-gate 			if (ret != DB_TXN_CKP)
183*7c478bd9Sstevel@tonic-gate 				goto msgerr;
184*7c478bd9Sstevel@tonic-gate 			else
185*7c478bd9Sstevel@tonic-gate 				ret = 0;
186*7c478bd9Sstevel@tonic-gate 	}
187*7c478bd9Sstevel@tonic-gate 	if (ret != 0 && ret != DB_NOTFOUND)
188*7c478bd9Sstevel@tonic-gate 		goto out;
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	/*
191*7c478bd9Sstevel@tonic-gate 	 * Pass #3.
192*7c478bd9Sstevel@tonic-gate 	 */
193*7c478bd9Sstevel@tonic-gate 	for (ret = log_get(lp, &lsn, &data, DB_NEXT);
194*7c478bd9Sstevel@tonic-gate 	    ret == 0; ret = log_get(lp, &lsn, &data, DB_NEXT)) {
195*7c478bd9Sstevel@tonic-gate 		if (dbenv->tx_recover != NULL)
196*7c478bd9Sstevel@tonic-gate 			ret = dbenv->tx_recover(lp,
197*7c478bd9Sstevel@tonic-gate 			    &data, &lsn, TXN_FORWARD_ROLL, txninfo);
198*7c478bd9Sstevel@tonic-gate 		else
199*7c478bd9Sstevel@tonic-gate 			ret = __db_dispatch(lp,
200*7c478bd9Sstevel@tonic-gate 			    &data, &lsn, TXN_FORWARD_ROLL, txninfo);
201*7c478bd9Sstevel@tonic-gate 		if (ret != 0)
202*7c478bd9Sstevel@tonic-gate 			if (ret != DB_TXN_CKP)
203*7c478bd9Sstevel@tonic-gate 				goto msgerr;
204*7c478bd9Sstevel@tonic-gate 			else
205*7c478bd9Sstevel@tonic-gate 				ret = 0;
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate 	if (ret != DB_NOTFOUND)
208*7c478bd9Sstevel@tonic-gate 		goto out;
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 	/* Now close all the db files that are open. */
211*7c478bd9Sstevel@tonic-gate 	__log_close_files(lp);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 	/*
214*7c478bd9Sstevel@tonic-gate 	 * Now set the last checkpoint lsn and the current time,
215*7c478bd9Sstevel@tonic-gate 	 * take a checkpoint, and reset the txnid.
216*7c478bd9Sstevel@tonic-gate 	 */
217*7c478bd9Sstevel@tonic-gate 	(void)time(&now);
218*7c478bd9Sstevel@tonic-gate 	dbenv->tx_info->region->last_ckp = ckp_lsn;
219*7c478bd9Sstevel@tonic-gate 	dbenv->tx_info->region->time_ckp = (u_int32_t)now;
220*7c478bd9Sstevel@tonic-gate 	if ((ret = txn_checkpoint(dbenv->tx_info, 0, 0)) != 0)
221*7c478bd9Sstevel@tonic-gate 		goto out;
222*7c478bd9Sstevel@tonic-gate 	dbenv->tx_info->region->last_txnid = TXN_MINIMUM;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	if (dbenv->db_verbose) {
225*7c478bd9Sstevel@tonic-gate 		__db_err(lp->dbenv, "Recovery complete at %.24s", ctime(&now));
226*7c478bd9Sstevel@tonic-gate 		__db_err(lp->dbenv, "%s %lx %s [%lu][%lu]",
227*7c478bd9Sstevel@tonic-gate 		    "Maximum transaction id",
228*7c478bd9Sstevel@tonic-gate 		    ((DB_TXNHEAD *)txninfo)->maxid,
229*7c478bd9Sstevel@tonic-gate 		    "Recovery checkpoint",
230*7c478bd9Sstevel@tonic-gate 		    (u_long)dbenv->tx_info->region->last_ckp.file,
231*7c478bd9Sstevel@tonic-gate 		    (u_long)dbenv->tx_info->region->last_ckp.offset);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	if (0) {
235*7c478bd9Sstevel@tonic-gate msgerr:		__db_err(dbenv, "Recovery function for LSN %lu %lu failed",
236*7c478bd9Sstevel@tonic-gate 		    (u_long)lsn.file, (u_long)lsn.offset);
237*7c478bd9Sstevel@tonic-gate 	}
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate out:	F_SET(lp, is_thread);
240*7c478bd9Sstevel@tonic-gate 	__db_txnlist_end(txninfo);
241*7c478bd9Sstevel@tonic-gate 	if (ckp_args != NULL)
242*7c478bd9Sstevel@tonic-gate 		__os_free(ckp_args, sizeof(*ckp_args));
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	return (ret);
245*7c478bd9Sstevel@tonic-gate }
246