summaryrefslogtreecommitdiff
path: root/src/log/log_verify.c
blob: e7f8f6889630b37babba560099a157aa0e0b022a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
390
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/btree.h"
#include "dbinc/fop.h"
#include "dbinc/hash.h"
#include "dbinc/qam.h"
#include "dbinc/txn.h"

#include "dbinc/log_verify.h"

#define	FIRST_OFFSET(env) \
	(sizeof(LOGP) + (CRYPTO_ON(env) ? HDR_CRYPTO_SZ : HDR_NORMAL_SZ))

static int __env_init_verify __P((ENV *, u_int32_t, DB_DISTAB *));

/*
 * PUBLIC: int __log_verify_pp __P((DB_ENV *, const DB_LOG_VERIFY_CONFIG *));
 */
int
__log_verify_pp(dbenv, lvconfig)
	DB_ENV *dbenv;
	const DB_LOG_VERIFY_CONFIG *lvconfig;
{
	int lsnrg, ret, timerg;
	DB_THREAD_INFO *ip;
	const char *phome;

	lsnrg = ret = timerg = 0;
	phome = NULL;

	if (!IS_ZERO_LSN(lvconfig->start_lsn) ||
	    !IS_ZERO_LSN(lvconfig->end_lsn))
		lsnrg = 1;
	if (lvconfig->start_time != 0 || lvconfig->end_time != 0)
		timerg = 1;

	if ((!IS_ZERO_LSN(lvconfig->start_lsn) && lvconfig->start_time != 0) ||
	    (!IS_ZERO_LSN(lvconfig->end_lsn) && lvconfig->end_time != 0) ||
	    (lsnrg && timerg)) {
		__db_errx(dbenv->env, DB_STR("2501",
		    "Set either an lsn range or a time range to verify logs "
		    "in the range, don't mix time and lsn."));
		ret = EINVAL;
		goto err;
	}
	phome = dbenv->env->db_home;
	if (phome != NULL && lvconfig->temp_envhome != NULL &&
	    strcmp(phome, lvconfig->temp_envhome) == 0) {
		__db_errx(dbenv->env,
		    "Environment home for log verification internal use "
		    "overlaps with that of the environment to verify.");
		ret = EINVAL;
		goto err;
	}

	ENV_ENTER(dbenv->env, ip);
	ret = __log_verify(dbenv, lvconfig, ip);
	ENV_LEAVE(dbenv->env, ip);
err:	return (ret);
}

/*
 * PUBLIC: int __log_verify __P((DB_ENV *, const DB_LOG_VERIFY_CONFIG *,
 * PUBLIC:     DB_THREAD_INFO *));
 */
int
__log_verify(dbenv, lvconfig, ip)
	DB_ENV *dbenv;
	const DB_LOG_VERIFY_CONFIG *lvconfig;
	DB_THREAD_INFO *ip;
{

	u_int32_t logcflag, max_fileno;
	DB_LOGC *logc;
	ENV *env;
	DBT data;
	DB_DISTAB dtab;
	DB_LSN key, start, start2, stop, stop2, verslsn;
	u_int32_t newversion, version;
	int cmp, fwdscroll, goprev, ret, tret;
	time_t starttime, endtime;
	const char *okmsg;
	DB_LOG_VRFY_INFO *logvrfy_hdl;

	okmsg = NULL;
	fwdscroll = 1;
	max_fileno = (u_int32_t)-1;
	goprev = 0;
	env = dbenv->env;
	logc = NULL;
	memset(&dtab, 0, sizeof(dtab));
	memset(&data, 0, sizeof(data));
	version = newversion = 0;
	ZERO_LSN(verslsn);
	memset(&start, 0, sizeof(DB_LSN));
	memset(&start2, 0, sizeof(DB_LSN));
	memset(&stop, 0, sizeof(DB_LSN));
	memset(&stop2, 0, sizeof(DB_LSN));
	memset(&key, 0, sizeof(DB_LSN));
	memset(&verslsn, 0, sizeof(DB_LSN));

	start = lvconfig->start_lsn;
	stop = lvconfig->end_lsn;
	starttime = lvconfig->start_time;
	endtime = lvconfig->end_time;

	if ((ret = __create_log_vrfy_info(lvconfig, &logvrfy_hdl, ip)) != 0)
		goto err;
	logvrfy_hdl->lv_config = lvconfig;
	if (lvconfig->continue_after_fail)
		F_SET(logvrfy_hdl, DB_LOG_VERIFY_CAF);
	if (lvconfig->verbose)
		F_SET(logvrfy_hdl, DB_LOG_VERIFY_VERBOSE);

	/* Allocate a log cursor. */
	if ((ret = __log_cursor(dbenv->env, &logc)) != 0) {
		__db_err(dbenv->env, ret, "DB_ENV->log_cursor");
		goto err;
	}
	/* Ignore failed chksum and go on with next one. */
	F_SET(logc->env->lg_handle, DBLOG_VERIFYING);

	/* Only scan the range that we want to verify. */
	if (fwdscroll) {
		if (IS_ZERO_LSN(stop)) {
			logcflag = DB_LAST;
			key.file = key.offset = 0;
		} else {
			key = stop;
			logcflag = DB_SET;
		}
		logvrfy_hdl->flags |= DB_LOG_VERIFY_FORWARD;
		goto startscroll;
	}

vrfyscroll:

	/*
	 * Initialize version to 0 so that we get the
	 * correct version right away.
	 */
	version = 0;
	ZERO_LSN(verslsn);

	/*
	 * In the log verification config struct, start_lsn and end_lsn have
	 * higher priority than start_time and end_time, and you can specify
	 * either lsn or time to start/stop verification.
	 */
	if (starttime != 0 || endtime != 0) {
		if ((ret = __find_lsnrg_by_timerg(logvrfy_hdl,
		    starttime, endtime, &start2, &stop2)) != 0)
			goto err;
		((DB_LOG_VERIFY_CONFIG *)lvconfig)->start_lsn = start = start2;
		((DB_LOG_VERIFY_CONFIG *)lvconfig)->end_lsn = stop = stop2;
	}

	if (IS_ZERO_LSN(start)) {
		logcflag = DB_FIRST;
		key.file = key.offset = 0;
	} else {
		key = start;
		logcflag = DB_SET;
		F_SET(logvrfy_hdl, DB_LOG_VERIFY_PARTIAL);
	}
	goprev = 0;

	/*
	 * So far we only support verifying a specific db file. The config's
	 * dbfile must be prefixed with the data directory if it's not in
	 * environment home directory.
	 */
	if (lvconfig->dbfile != NULL) {
		F_SET(logvrfy_hdl,
		    DB_LOG_VERIFY_DBFILE | DB_LOG_VERIFY_PARTIAL);
		if ((ret = __set_logvrfy_dbfuid(logvrfy_hdl)) != 0)
			goto err;
	}

startscroll:

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

	for (;;) {

		/*
		 * We may have reached beyond the range we're verifying.
		 */
		if (!fwdscroll && !IS_ZERO_LSN(stop)) {
			cmp = LOG_COMPARE(&key, &stop);
			if (cmp > 0)
				break;
		}
		if (fwdscroll && !IS_ZERO_LSN(start)) {
			cmp = LOG_COMPARE(&key, &start);
			if (cmp < 0)
				break;
		}

		ret = __logc_get(logc, &key, &data, logcflag);
		if (ret != 0) {
			if (ret == DB_NOTFOUND) {
				/* We may not start from the first log file. */
				if (logcflag == DB_PREV && key.file > 1)
					F_SET(logvrfy_hdl,
					    DB_LOG_VERIFY_PARTIAL);
				break;
			}
			__db_err(dbenv->env, ret, "DB_LOGC->get");
			/*
			 * When go beyond valid lsn range, we may get other
			 * error values than DB_NOTFOUND.
			 */
			goto out;
		}

		if (logcflag == DB_SET) {
			if (goprev)
				logcflag = DB_PREV;
			else
				logcflag = DB_NEXT;
		} else if (logcflag == DB_LAST) {
			logcflag = DB_PREV;
			max_fileno = key.file;
		} else if (logcflag == DB_FIRST)
			logcflag = DB_NEXT;

		if (key.file != verslsn.file) {
			/*
			 * If our log file changed, we need to see if the
			 * version of the log file changed as well.
			 * If it changed, reset the print table.
			 */
			if ((ret = __logc_version(logc, &newversion)) != 0) {
				__db_err(dbenv->env, ret, "DB_LOGC->version");
				goto err;
			}
			if (version != newversion) {
				version = newversion;
				if (!IS_LOG_VRFY_SUPPORTED(version)) {
					__db_msg(dbenv->env, DB_STR_A("2502",
				"[%lu][%lu] Unsupported version of log file, "
				"log file number: %u, log file version: %u, "
				"supported log version: %u.",
					    "%lu %lu %u %u %u"),
					    (u_long)key.file,
					    (u_long)key.offset,
					    key.file, version, DB_LOGVERSION);
					if (logcflag == DB_NEXT) {
						key.file += 1;
						if (key.file > max_fileno)
							break;
				/*
				 * Txns don't span log versions, no need to
				 * set DB_LOG_VERIFY_PARTIAL here.
				 */
					} else {
						goprev = 1;
						key.file -= 1;
						if (key.file == 0)
							break;
					}
					key.offset = FIRST_OFFSET(env);
					logcflag = DB_SET;
					continue;
				}
				if ((ret = __env_init_verify(env, version,
				    &dtab)) != 0) {
					__db_err(dbenv->env, ret,
					    DB_STR("2503",
					    "callback: initialization"));
					goto err;
				}
			}
			verslsn = key;
		}

		ret = __db_dispatch(dbenv->env, &dtab, &data, &key,
		    DB_TXN_LOG_VERIFY, logvrfy_hdl);

		if (!fwdscroll && ret != 0) {
			if (!F_ISSET(logvrfy_hdl, DB_LOG_VERIFY_CAF)) {
				__db_err(dbenv->env, ret,
				    "[%lu][%lu] __db_dispatch",
				    (u_long)key.file, (u_long)key.offset);
				goto err;
			} else
				F_SET(logvrfy_hdl, DB_LOG_VERIFY_ERR);
		}
	}

	if (fwdscroll) {
		fwdscroll = 0;
		F_CLR(logvrfy_hdl, DB_LOG_VERIFY_FORWARD);
		goto vrfyscroll;
	}
out:
	/*
	 * When we arrive here ret can be 0 or errors returned by DB_LOGC->get,
	 * all which we have already handled. So we clear ret.
	 */
	ret = 0;

	/* If continuing after fail, we can complete the entire log. */
	if (F_ISSET(logvrfy_hdl, DB_LOG_VERIFY_ERR) ||
	    F_ISSET(logvrfy_hdl, DB_LOG_VERIFY_INTERR))
		ret = DB_LOG_VERIFY_BAD;
	/*
	 * This function can be called when the environment is alive, so
	 * there can be active transactions.
	 */
	__db_log_verify_global_report(logvrfy_hdl);
	if (ret == DB_LOG_VERIFY_BAD)
		okmsg = DB_STR_P("FAILED");
	else {
		DB_ASSERT(dbenv->env, ret == 0);
		okmsg = DB_STR_P("SUCCEEDED");
	}

	__db_msg(dbenv->env, DB_STR_A("2504",
	    "Log verification ended and %s.", "%s"), okmsg);

err:
	if (logc != NULL)
		(void)__logc_close(logc);
	if ((tret = __destroy_log_vrfy_info(logvrfy_hdl)) != 0 && ret == 0)
		ret = tret;
	if (dtab.int_dispatch)
		__os_free(dbenv->env, dtab.int_dispatch);
	if (dtab.ext_dispatch)
		__os_free(dbenv->env, dtab.ext_dispatch);

	return (ret);
}

/*
 * __env_init_verify--
 */
static int
__env_init_verify(env, version, dtabp)
	ENV *env;
	u_int32_t version;
	DB_DISTAB *dtabp;
{
	int ret;

	/*
	 * We need to prime the print table with the current print
	 * functions.  Then we overwrite only specific entries based on
	 * each previous version we support.
	 */
	if ((ret = __bam_init_verify(env, dtabp)) != 0)
		goto err;
	if ((ret = __crdel_init_verify(env, dtabp)) != 0)
		goto err;
	if ((ret = __db_init_verify(env, dtabp)) != 0)
		goto err;
	if ((ret = __dbreg_init_verify(env, dtabp)) != 0)
		goto err;
	if ((ret = __fop_init_verify(env, dtabp)) != 0)
		goto err;
#ifdef HAVE_HASH
	if ((ret = __ham_init_verify(env, dtabp)) != 0)
		goto err;
#endif
#ifdef HAVE_HEAP
	if ((ret = __heap_init_verify(env, dtabp)) != 0)
		goto err;
#endif
#ifdef HAVE_QUEUE
	if ((ret = __qam_init_verify(env, dtabp)) != 0)
		goto err;
#endif
	if ((ret = __txn_init_verify(env, dtabp)) != 0)
		goto err;

	switch (version) {
	case DB_LOGVERSION:
		ret = 0;
		break;

	default:
		__db_errx(env, DB_STR_A("2505", "Not supported version %lu",
		    "%lu"), (u_long)version);
		ret = EINVAL;
		break;
	}
err:	return (ret);
}

/*
 * __log_verify_wrap --
 *      Wrapper function for APIs of other languages, like java/c# and
 *      script languages. It's much easier to implement the swig layer
 *      when we split up the C structure.
 *
 * PUBLIC: int __log_verify_wrap __P((ENV *, const char *, u_int32_t,
 * PUBLIC:     const char *, const char *, time_t, time_t, u_int32_t,
 * PUBLIC:     u_int32_t, u_int32_t, u_int32_t, int, int));
 */
int
__log_verify_wrap(env, envhome, cachesize, dbfile, dbname,
    stime, etime, stfile, stoffset, efile, eoffset, caf, verbose)
	ENV *env;
	const char *envhome, *dbfile, *dbname;
	time_t stime, etime;
	u_int32_t cachesize, stfile, stoffset, efile, eoffset;
	int caf, verbose;
{
	DB_LOG_VERIFY_CONFIG cfg;

	memset(&cfg, 0, sizeof(cfg));
	cfg.cachesize = cachesize;
	cfg.temp_envhome = envhome;
	cfg.dbfile = dbfile;
	cfg.dbname = dbname;
	cfg.start_time = stime;
	cfg.end_time = etime;
	cfg.start_lsn.file = stfile;
	cfg.start_lsn.offset = stoffset;
	cfg.end_lsn.file = efile;
	cfg.end_lsn.offset = eoffset;
	cfg.continue_after_fail = caf;
	cfg.verbose = verbose;

	return __log_verify_pp(env->dbenv, &cfg);
}