about summary refs log tree commit diff
path: root/db2/include/log.h
blob: 50309085aa783b28b979db18b154664a5ef2c628 (plain) (blame)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997, 1998
 *	Sleepycat Software.  All rights reserved.
 *
 *	@(#)log.h	10.30 (Sleepycat) 10/11/98
 */

#ifndef _LOG_H_
#define	_LOG_H_

struct __fname;		typedef struct __fname FNAME;
struct __hdr;		typedef struct __hdr HDR;
struct __log;		typedef struct __log LOG;
struct __log_persist;	typedef struct __log_persist LOGP;

#ifndef MAXLFNAME
#define	LFPREFIX	"log."		/* Log file name prefix. */
#define	LFNAME		"log.%010d"	/* Log file name template. */
#define	LFNAME_V1	"log.%05d"	/* Log file name template, rev 1. */
#define	MAXLFNAME	2000000000	/* Maximum log file name. */
#endif
					/* Default log name. */
#define DB_DEFAULT_LOG_FILE	"__db_log.share"

#define	DEFAULT_MAX	(10 * MEGABYTE)	/* 10 Mb. */

/* Macros to lock/unlock the region and threads. */
#define	LOCK_LOGTHREAD(dblp)						\
	if (F_ISSET(dblp, DB_AM_THREAD))				\
		(void)__db_mutex_lock((dblp)->mutexp, -1)
#define	UNLOCK_LOGTHREAD(dblp)						\
	if (F_ISSET(dblp, DB_AM_THREAD))				\
		(void)__db_mutex_unlock((dblp)->mutexp, -1);
#define	LOCK_LOGREGION(dblp)						\
	(void)__db_mutex_lock(&((RLAYOUT *)(dblp)->lp)->lock,		\
	    (dblp)->reginfo.fd)
#define	UNLOCK_LOGREGION(dblp)						\
	(void)__db_mutex_unlock(&((RLAYOUT *)(dblp)->lp)->lock,		\
	    (dblp)->reginfo.fd)

/* Check for region catastrophic shutdown. */
#define	LOG_PANIC_CHECK(dblp) {						\
	if ((dblp)->lp->rlayout.panic)					\
		return (DB_RUNRECOVERY);				\
}

/*
 * The per-process table that maps log file-id's to DB structures.
 */
typedef	struct __db_entry {
	DB	 *dbp;			/* Associated DB structure. */
	u_int32_t refcount;		/* Reference counted. */
	int	  deleted;		/* File was not found during open. */
} DB_ENTRY;

/*
 * DB_LOG
 *	Per-process log structure.
 */
struct __db_log {
/* These fields need to be protected for multi-threaded support. */
	db_mutex_t	*mutexp;	/* Mutex for thread protection. */

	DB_ENTRY *dbentry;		/* Recovery file-id mapping. */
#define	DB_GROW_SIZE	64
	u_int32_t dbentry_cnt;		/* Entries.  Grows by DB_GROW_SIZE. */

/*
 * These fields are always accessed while the region lock is held, so they do
 * not have to be protected by the thread lock as well OR, they are only used
 * when threads are not being used, i.e. most cursor operations are disallowed
 * on threaded logs.
 */
	u_int32_t lfname;		/* Log file "name". */
	int	  lfd;			/* Log file descriptor. */

	DB_LSN	  c_lsn;		/* Cursor: current LSN. */
	DBT	  c_dbt;		/* Cursor: return DBT structure. */
	int	  c_fd;			/* Cursor: file descriptor. */
	u_int32_t c_off;		/* Cursor: previous record offset. */
	u_int32_t c_len;		/* Cursor: current record length. */

/* These fields are not protected. */
	LOG	 *lp;			/* Address of the shared LOG. */

	DB_ENV	 *dbenv;		/* Reference to error information. */
	REGINFO	  reginfo;		/* Region information. */

	void     *addr;			/* Address of shalloc() region. */

	char	 *dir;			/* Directory argument. */

/*
 * These fields are used by XA; since XA forbids threaded execution, these
 * do not have to be protected.
 */
	void 	*xa_info;		/* Committed transaction list that
					 * has to be carried between calls
					 * to xa_recover. */
	DB_LSN	xa_lsn;			/* Position of an XA recovery scan. */
	DB_LSN	xa_first;		/* LSN to which we need to roll back
					   for this XA recovery scan. */

	/*
	 * !!!
	 * Currently used to hold:
	 *	DB_AM_THREAD	(a DB flag)
	 *	DBC_RECOVER	(a DBC flag)
	 * If they are ever the same bits, we're in serious trouble.
	 */
#if DB_AM_THREAD == DBC_RECOVER
	DB_AM_THREAD, DBC_RECOVER, FLAG MISMATCH
#endif
	u_int32_t flags;
};

/*
 * HDR --
 *	Log record header.
 */
struct __hdr {
	u_int32_t prev;			/* Previous offset. */
	u_int32_t cksum;		/* Current checksum. */
	u_int32_t len;			/* Current length. */
};

struct __log_persist {
	u_int32_t magic;		/* DB_LOGMAGIC */
	u_int32_t version;		/* DB_LOGVERSION */

	u_int32_t lg_max;		/* Maximum file size. */
	int	  mode;			/* Log file mode. */
};

/*
 * LOG --
 *	Shared log region.  One of these is allocated in shared memory,
 *	and describes the log.
 */
struct __log {
	RLAYOUT	  rlayout;		/* General region information. */

	LOGP	  persist;		/* Persistent information. */

	SH_TAILQ_HEAD(__fq) fq;		/* List of file names. */

	/*
	 * The lsn LSN is the file offset that we're about to write and which
	 * we will return to the user.
	 */
	DB_LSN	  lsn;			/* LSN at current file offset. */

	/*
	 * The s_lsn LSN is the last LSN that we know is on disk, not just
	 * written, but synced.
	 */
	DB_LSN	  s_lsn;		/* LSN of the last sync. */

	u_int32_t len;			/* Length of the last record. */

	u_int32_t w_off;		/* Current write offset in the file. */

	DB_LSN	  chkpt_lsn;		/* LSN of the last checkpoint. */
	time_t	  chkpt;		/* Time of the last checkpoint. */

	DB_LOG_STAT stat;		/* Log statistics. */

	/*
	 * The f_lsn LSN is the LSN (returned to the user) that "owns" the
	 * first byte of the buffer.  If the record associated with the LSN
	 * spans buffers, it may not reflect the physical file location of
	 * the first byte of the buffer.
	 */
	DB_LSN	  f_lsn;		/* LSN of first byte in the buffer. */
	size_t	  b_off;		/* Current offset in the buffer. */
	u_int8_t buf[4 * 1024];		/* Log buffer. */
};

/*
 * FNAME --
 *	File name and id.
 */
struct __fname {
	SH_TAILQ_ENTRY q;		/* File name queue. */

	u_int16_t ref;			/* Reference count. */

	u_int32_t id;			/* Logging file id. */
	DBTYPE	  s_type;		/* Saved DB type. */

	size_t	  name_off;		/* Name offset. */
	u_int8_t  ufid[DB_FILE_ID_LEN];	/* Unique file id. */
};

/* File open/close register log record opcodes. */
#define	LOG_CHECKPOINT	1		/* Checkpoint: file name/id dump. */
#define	LOG_CLOSE	2		/* File close. */
#define	LOG_OPEN	3		/* File open. */

#include "log_auto.h"
#include "log_ext.h"
#endif /* _LOG_H_ */