1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1997,2008 Oracle.  All rights reserved.
5 *
6 * $Id: os.h,v 12.31 2008/03/26 04:11:34 david Exp $
7 */
8
9#ifndef _DB_OS_H_
10#define	_DB_OS_H_
11
12#if defined(__cplusplus)
13extern "C" {
14#endif
15
16/* Number of times to retry system calls that return EINTR or EBUSY. */
17#define	DB_RETRY	100
18
19#ifdef __TANDEM
20/*
21 * OSS Tandem problem: fsync can return a Guardian file system error of 70,
22 * which has no symbolic name in OSS.  HP says to retry the fsync. [#12957]
23 */
24#define	RETRY_CHK(op, ret) do {						\
25	int __retries, __t_ret;						\
26	for ((ret) = 0, __retries = DB_RETRY;;) {			\
27		if ((op) == 0)						\
28			break;						\
29		(ret) = __os_get_syserr();				\
30		if (((__t_ret = __os_posix_err(ret)) == EAGAIN ||	\
31		    __t_ret == EBUSY || __t_ret == EINTR ||		\
32		    __t_ret == EIO || __t_ret == 70) && --__retries > 0)\
33			continue;					\
34		break;							\
35	}								\
36} while (0)
37#else
38#define	RETRY_CHK(op, ret) do {						\
39	int __retries, __t_ret;						\
40	for ((ret) = 0, __retries = DB_RETRY;;) {			\
41		if ((op) == 0)						\
42			break;						\
43		(ret) = __os_get_syserr();				\
44		if (((__t_ret = __os_posix_err(ret)) == EAGAIN ||	\
45		    __t_ret == EBUSY || __t_ret == EINTR ||		\
46		    __t_ret == EIO) && --__retries > 0)			\
47			continue;					\
48		break;							\
49	}								\
50} while (0)
51#endif
52
53#define	RETRY_CHK_EINTR_ONLY(op, ret) do {				\
54	int __retries;							\
55	for ((ret) = 0, __retries = DB_RETRY;;) {			\
56		if ((op) == 0)						\
57			break;						\
58		(ret) = __os_get_syserr();				\
59		if (__os_posix_err(ret) == EINTR && --__retries > 0)	\
60			continue;					\
61		break;							\
62	}								\
63} while (0)
64
65/*
66 * Flags understood by __os_open.
67 */
68#define	DB_OSO_ABSMODE	0x0001		/* Absolute mode specified. */
69#define	DB_OSO_CREATE	0x0002		/* POSIX: O_CREAT */
70#define	DB_OSO_DIRECT	0x0004		/* Don't buffer the file in the OS. */
71#define	DB_OSO_DSYNC	0x0008		/* POSIX: O_DSYNC. */
72#define	DB_OSO_EXCL	0x0010		/* POSIX: O_EXCL */
73#define	DB_OSO_RDONLY	0x0020		/* POSIX: O_RDONLY */
74#define	DB_OSO_REGION	0x0040		/* Opening a region file. */
75#define	DB_OSO_SEQ	0x0080		/* Expected sequential access. */
76#define	DB_OSO_TEMP	0x0100		/* Remove after last close. */
77#define	DB_OSO_TRUNC	0x0200		/* POSIX: O_TRUNC */
78
79/*
80 * File modes.
81 */
82#define	DB_MODE_400	(S_IRUSR)
83#define	DB_MODE_600	(S_IRUSR|S_IWUSR)
84#define	DB_MODE_660	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)
85#define	DB_MODE_666	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
86#define	DB_MODE_700	(S_IRUSR|S_IWUSR|S_IXUSR)
87
88/*
89 * We group certain seek/write calls into a single function so that we
90 * can use pread(2)/pwrite(2) where they're available.
91 */
92#define	DB_IO_READ	1
93#define	DB_IO_WRITE	2
94
95/*
96 * Make a last "panic" check.  Imagine a thread of control running in Berkeley
97 * DB, going to sleep.  Another thread of control decides to run recovery
98 * because the environment is broken.  The first thing recovery does is panic
99 * the existing environment, but we only check the panic flag when crossing the
100 * public API.  If the sleeping thread wakes up and writes something, we could
101 * have two threads of control writing the log files at the same time.  So,
102 * before reading or writing, make a last panic check.  Obviously, there's still
103 * a window, but it's very, very small.
104 */
105#define	LAST_PANIC_CHECK_BEFORE_IO(env)					\
106	PANIC_CHECK(env);
107
108/* DB filehandle. */
109struct __fh_t {
110	/*
111	 * Linked list of DB_FH's, linked from the DB_ENV, used to keep track
112	 * of all open file handles for resource cleanup.
113	 */
114	 TAILQ_ENTRY(__fh_t) q;
115
116	/*
117	 * The file-handle mutex is only used to protect the handle/fd
118	 * across seek and read/write pairs, it does not protect the
119	 * the reference count, or any other fields in the structure.
120	 */
121	db_mutex_t mtx_fh;		/* Mutex to lock. */
122
123	int	ref;			/* Reference count. */
124
125#ifdef HAVE_BREW
126	IFile	*ifp;			/* IFile pointer */
127#endif
128#if defined(DB_WIN32)
129	HANDLE	handle;			/* Windows/32 file handle. */
130	HANDLE	trunc_handle;		/* Handle for truncate calls. */
131#endif
132	int	fd;			/* POSIX file descriptor. */
133
134	char	*name;			/* File name at open. */
135
136	/*
137	 * Last seek statistics, used for zero-filling on filesystems
138	 * that don't support it directly.
139	 */
140	db_pgno_t pgno;
141	u_int32_t pgsize;
142	u_int32_t offset;
143
144#ifdef HAVE_STATISTICS
145	u_int32_t seek_count;		/* I/O statistics */
146	u_int32_t read_count;
147	u_int32_t write_count;
148#endif
149
150#define	DB_FH_ENVLINK	0x01		/* We're linked on the DB_ENV. */
151#define	DB_FH_NOSYNC	0x02		/* Handle doesn't need to be sync'd. */
152#define	DB_FH_OPENED	0x04		/* Handle is valid. */
153#define	DB_FH_UNLINK	0x08		/* Unlink on close */
154#define	DB_FH_REGION	0x10		/* Opened to contain a region */
155	u_int8_t flags;
156};
157
158/* Standard buffer size for ctime/ctime_r function calls. */
159#define	CTIME_BUFLEN	26
160
161/*
162 * VxWorks requires we cast (const char *) variables to (char *) in order to
163 * pass them to system calls like stat, read and write.
164 */
165#ifdef HAVE_VXWORKS
166#define	CHAR_STAR_CAST	(char *)
167#else
168#define	CHAR_STAR_CAST
169#endif
170
171#if defined(__cplusplus)
172}
173#endif
174
175#include "dbinc_auto/os_ext.h"
176#endif /* !_DB_OS_H_ */
177