17c478bdstevel@tonic-gate/*-
27c478bdstevel@tonic-gate * See the file LICENSE for redistribution information.
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * Copyright (c) 1996, 1997, 1998
57c478bdstevel@tonic-gate *	Sleepycat Software.  All rights reserved.
67c478bdstevel@tonic-gate *
77c478bdstevel@tonic-gate *	@(#)log.h	10.30 (Sleepycat) 10/11/98
87c478bdstevel@tonic-gate */
97c478bdstevel@tonic-gate
107c478bdstevel@tonic-gate#ifndef _LOG_H_
117c478bdstevel@tonic-gate#define	_LOG_H_
127c478bdstevel@tonic-gate
137c478bdstevel@tonic-gatestruct __fname;		typedef struct __fname FNAME;
147c478bdstevel@tonic-gatestruct __hdr;		typedef struct __hdr HDR;
157c478bdstevel@tonic-gatestruct __log;		typedef struct __log LOG;
167c478bdstevel@tonic-gatestruct __log_persist;	typedef struct __log_persist LOGP;
177c478bdstevel@tonic-gate
187c478bdstevel@tonic-gate#ifndef MAXLFNAME
197c478bdstevel@tonic-gate#define	LFPREFIX	"log."		/* Log file name prefix. */
207c478bdstevel@tonic-gate#define	LFNAME		"log.%010d"	/* Log file name template. */
217c478bdstevel@tonic-gate#define	LFNAME_V1	"log.%05d"	/* Log file name template, rev 1. */
227c478bdstevel@tonic-gate#define	MAXLFNAME	2000000000	/* Maximum log file name. */
237c478bdstevel@tonic-gate#endif
247c478bdstevel@tonic-gate					/* Default log name. */
257c478bdstevel@tonic-gate#define DB_DEFAULT_LOG_FILE	"__db_log.share"
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate#define	DEFAULT_MAX	(10 * MEGABYTE)	/* 10 Mb. */
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate/* Macros to lock/unlock the region and threads. */
307c478bdstevel@tonic-gate#define	LOCK_LOGTHREAD(dblp)						\
317c478bdstevel@tonic-gate	if (F_ISSET(dblp, DB_AM_THREAD))				\
327c478bdstevel@tonic-gate		(void)__db_mutex_lock((dblp)->mutexp, -1)
337c478bdstevel@tonic-gate#define	UNLOCK_LOGTHREAD(dblp)						\
347c478bdstevel@tonic-gate	if (F_ISSET(dblp, DB_AM_THREAD))				\
357c478bdstevel@tonic-gate		(void)__db_mutex_unlock((dblp)->mutexp, -1);
367c478bdstevel@tonic-gate#define	LOCK_LOGREGION(dblp)						\
377c478bdstevel@tonic-gate	(void)__db_mutex_lock(&((RLAYOUT *)(dblp)->lp)->lock,		\
387c478bdstevel@tonic-gate	    (dblp)->reginfo.fd)
397c478bdstevel@tonic-gate#define	UNLOCK_LOGREGION(dblp)						\
407c478bdstevel@tonic-gate	(void)__db_mutex_unlock(&((RLAYOUT *)(dblp)->lp)->lock,		\
417c478bdstevel@tonic-gate	    (dblp)->reginfo.fd)
427c478bdstevel@tonic-gate
437c478bdstevel@tonic-gate/* Check for region catastrophic shutdown. */
447c478bdstevel@tonic-gate#define	LOG_PANIC_CHECK(dblp) {						\
457c478bdstevel@tonic-gate	if ((dblp)->lp->rlayout.panic)					\
467c478bdstevel@tonic-gate		return (DB_RUNRECOVERY);				\
477c478bdstevel@tonic-gate}
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate/*
507c478bdstevel@tonic-gate * The per-process table that maps log file-id's to DB structures.
517c478bdstevel@tonic-gate */
527c478bdstevel@tonic-gatetypedef	struct __db_entry {
537c478bdstevel@tonic-gate	DB	 *dbp;			/* Associated DB structure. */
547c478bdstevel@tonic-gate	char 	 *name;			/* File name. */
557c478bdstevel@tonic-gate	u_int32_t refcount;		/* Reference counted. */
567c478bdstevel@tonic-gate	int	  deleted;		/* File was not found during open. */
577c478bdstevel@tonic-gate} DB_ENTRY;
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate/*
607c478bdstevel@tonic-gate * DB_LOG
617c478bdstevel@tonic-gate *	Per-process log structure.
627c478bdstevel@tonic-gate */
637c478bdstevel@tonic-gatestruct __db_log {
647c478bdstevel@tonic-gate/* These fields need to be protected for multi-threaded support. */
657c478bdstevel@tonic-gate	db_mutex_t	*mutexp;	/* Mutex for thread protection. */
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate	DB_ENTRY *dbentry;		/* Recovery file-id mapping. */
687c478bdstevel@tonic-gate#define	DB_GROW_SIZE	64
697c478bdstevel@tonic-gate	u_int32_t dbentry_cnt;		/* Entries.  Grows by DB_GROW_SIZE. */
707c478bdstevel@tonic-gate
717c478bdstevel@tonic-gate/*
727c478bdstevel@tonic-gate * These fields are always accessed while the region lock is held, so they do
737c478bdstevel@tonic-gate * not have to be protected by the thread lock as well OR, they are only used
747c478bdstevel@tonic-gate * when threads are not being used, i.e. most cursor operations are disallowed
757c478bdstevel@tonic-gate * on threaded logs.
767c478bdstevel@tonic-gate */
777c478bdstevel@tonic-gate	u_int32_t lfname;		/* Log file "name". */
787c478bdstevel@tonic-gate	int	  lfd;			/* Log file descriptor. */
797c478bdstevel@tonic-gate
807c478bdstevel@tonic-gate	DB_LSN	  c_lsn;		/* Cursor: current LSN. */
817c478bdstevel@tonic-gate	DBT	  c_dbt;		/* Cursor: return DBT structure. */
827c478bdstevel@tonic-gate	int	  c_fd;			/* Cursor: file descriptor. */
837c478bdstevel@tonic-gate	u_int32_t c_off;		/* Cursor: previous record offset. */
847c478bdstevel@tonic-gate	u_int32_t c_len;		/* Cursor: current record length. */
857c478bdstevel@tonic-gate
867c478bdstevel@tonic-gate/* These fields are not protected. */
877c478bdstevel@tonic-gate	LOG	 *lp;			/* Address of the shared LOG. */
887c478bdstevel@tonic-gate
897c478bdstevel@tonic-gate	DB_ENV	 *dbenv;		/* Reference to error information. */
907c478bdstevel@tonic-gate	REGINFO	  reginfo;		/* Region information. */
917c478bdstevel@tonic-gate
927c478bdstevel@tonic-gate	void     *addr;			/* Address of shalloc() region. */
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate	char	 *dir;			/* Directory argument. */
957c478bdstevel@tonic-gate
967c478bdstevel@tonic-gate/*
977c478bdstevel@tonic-gate * These fields are used by XA; since XA forbids threaded execution, these
987c478bdstevel@tonic-gate * do not have to be protected.
997c478bdstevel@tonic-gate */
1007c478bdstevel@tonic-gate	void 	*xa_info;		/* Committed transaction list that
1017c478bdstevel@tonic-gate					 * has to be carried between calls
1027c478bdstevel@tonic-gate					 * to xa_recover. */
1037c478bdstevel@tonic-gate	DB_LSN	xa_lsn;			/* Position of an XA recovery scan. */
1047c478bdstevel@tonic-gate	DB_LSN	xa_first;		/* LSN to which we need to roll back
1057c478bdstevel@tonic-gate					   for this XA recovery scan. */
1067c478bdstevel@tonic-gate
1077c478bdstevel@tonic-gate	/*
1087c478bdstevel@tonic-gate	 * !!!
1097c478bdstevel@tonic-gate	 * Currently used to hold:
1107c478bdstevel@tonic-gate	 *	DB_AM_THREAD	(a DB flag)
1117c478bdstevel@tonic-gate	 *	DBC_RECOVER	(a DBC flag)
1127c478bdstevel@tonic-gate	 * If they are ever the same bits, we're in serious trouble.
1137c478bdstevel@tonic-gate	 */
1147c478bdstevel@tonic-gate#if DB_AM_THREAD == DBC_RECOVER
1157c478bdstevel@tonic-gate	DB_AM_THREAD, DBC_RECOVER, FLAG MISMATCH
1167c478bdstevel@tonic-gate#endif
1177c478bdstevel@tonic-gate	u_int32_t flags;
1187c478bdstevel@tonic-gate};
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate/*
1217c478bdstevel@tonic-gate * HDR --
1227c478bdstevel@tonic-gate *	Log record header.
1237c478bdstevel@tonic-gate */
1247c478bdstevel@tonic-gatestruct __hdr {
1257c478bdstevel@tonic-gate	u_int32_t prev;			/* Previous offset. */
1267c478bdstevel@tonic-gate	u_int32_t cksum;		/* Current checksum. */
1277c478bdstevel@tonic-gate	u_int32_t len;			/* Current length. */
1287c478bdstevel@tonic-gate};
1297c478bdstevel@tonic-gate
1307c478bdstevel@tonic-gatestruct __log_persist {
1317c478bdstevel@tonic-gate	u_int32_t magic;		/* DB_LOGMAGIC */
1327c478bdstevel@tonic-gate	u_int32_t version;		/* DB_LOGVERSION */
1337c478bdstevel@tonic-gate
1347c478bdstevel@tonic-gate	u_int32_t lg_max;		/* Maximum file size. */
1357c478bdstevel@tonic-gate	int	  mode;			/* Log file mode. */
1367c478bdstevel@tonic-gate};
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate/*
1397c478bdstevel@tonic-gate * LOG --
1407c478bdstevel@tonic-gate *	Shared log region.  One of these is allocated in shared memory,
1417c478bdstevel@tonic-gate *	and describes the log.
1427c478bdstevel@tonic-gate */
1437c478bdstevel@tonic-gatestruct __log {
1447c478bdstevel@tonic-gate	RLAYOUT	  rlayout;		/* General region information. */
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gate	LOGP	  persist;		/* Persistent information. */
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate	SH_TAILQ_HEAD(__fq) fq;		/* List of file names. */
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate	/*
1517c478bdstevel@tonic-gate	 * The lsn LSN is the file offset that we're about to write and which
1527c478bdstevel@tonic-gate	 * we will return to the user.
1537c478bdstevel@tonic-gate	 */
1547c478bdstevel@tonic-gate	DB_LSN	  lsn;			/* LSN at current file offset. */
1557c478bdstevel@tonic-gate
1567c478bdstevel@tonic-gate	/*
1577c478bdstevel@tonic-gate	 * The s_lsn LSN is the last LSN that we know is on disk, not just
1587c478bdstevel@tonic-gate	 * written, but synced.
1597c478bdstevel@tonic-gate	 */
1607c478bdstevel@tonic-gate	DB_LSN	  s_lsn;		/* LSN of the last sync. */
1617c478bdstevel@tonic-gate
1627c478bdstevel@tonic-gate	u_int32_t len;			/* Length of the last record. */
1637c478bdstevel@tonic-gate
1647c478bdstevel@tonic-gate	u_int32_t w_off;		/* Current write offset in the file. */
1657c478bdstevel@tonic-gate
1667c478bdstevel@tonic-gate	DB_LSN	  chkpt_lsn;		/* LSN of the last checkpoint. */
1677c478bdstevel@tonic-gate	time_t	  chkpt;		/* Time of the last checkpoint. */
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate	DB_LOG_STAT stat;		/* Log statistics. */
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate	/*
1727c478bdstevel@tonic-gate	 * The f_lsn LSN is the LSN (returned to the user) that "owns" the
1737c478bdstevel@tonic-gate	 * first byte of the buffer.  If the record associated with the LSN
1747c478bdstevel@tonic-gate	 * spans buffers, it may not reflect the physical file location of
1757c478bdstevel@tonic-gate	 * the first byte of the buffer.
1767c478bdstevel@tonic-gate	 */
1777c478bdstevel@tonic-gate	DB_LSN	  f_lsn;		/* LSN of first byte in the buffer. */
1787c478bdstevel@tonic-gate	size_t	  b_off;		/* Current offset in the buffer. */
1797c478bdstevel@tonic-gate	u_int8_t buf[4 * 1024];		/* Log buffer. */
1807c478bdstevel@tonic-gate};
1817c478bdstevel@tonic-gate
1827c478bdstevel@tonic-gate/*
1837c478bdstevel@tonic-gate * FNAME --
1847c478bdstevel@tonic-gate *	File name and id.
1857c478bdstevel@tonic-gate */
1867c478bdstevel@tonic-gatestruct __fname {
1877c478bdstevel@tonic-gate	SH_TAILQ_ENTRY q;		/* File name queue. */
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate	u_int16_t ref;			/* Reference count. */
1907c478bdstevel@tonic-gate
1917c478bdstevel@tonic-gate	u_int32_t id;			/* Logging file id. */
1927c478bdstevel@tonic-gate	DBTYPE	  s_type;		/* Saved DB type. */
1937c478bdstevel@tonic-gate
1947c478bdstevel@tonic-gate	size_t	  name_off;		/* Name offset. */
1957c478bdstevel@tonic-gate	u_int8_t  ufid[DB_FILE_ID_LEN];	/* Unique file id. */
1967c478bdstevel@tonic-gate};
1977c478bdstevel@tonic-gate
1987c478bdstevel@tonic-gate/* File open/close register log record opcodes. */
1997c478bdstevel@tonic-gate#define	LOG_CHECKPOINT	1		/* Checkpoint: file name/id dump. */
2007c478bdstevel@tonic-gate#define	LOG_CLOSE	2		/* File close. */
2017c478bdstevel@tonic-gate#define	LOG_OPEN	3		/* File open. */
2027c478bdstevel@tonic-gate
2037c478bdstevel@tonic-gate#include "log_auto.h"
2047c478bdstevel@tonic-gate#include "log_ext.h"
2057c478bdstevel@tonic-gate#endif /* _LOG_H_ */
206