summaryrefslogtreecommitdiff
path: root/db/log/log_method.c
blob: 1565a53a07792835fef20349ec0520ffda429c23 (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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1999-2004
 *	Sleepycat Software.  All rights reserved.
 *
 * $Id: log_method.c,v 11.50 2004/09/22 16:26:15 bostic Exp $
 */

#include "db_config.h"

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>

#ifdef HAVE_RPC
#include <rpc/rpc.h>
#endif

#include <stdlib.h>
#include <string.h>
#endif

#ifdef HAVE_RPC
#include "db_server.h"
#endif

#include "db_int.h"
#include "dbinc/log.h"

#ifdef HAVE_RPC
#include "dbinc_auto/rpc_client_ext.h"
#endif

static int __log_get_lg_bsize __P((DB_ENV *, u_int32_t *));
static int __log_get_lg_dir __P((DB_ENV *, const char **));
static int __log_get_lg_max __P((DB_ENV *, u_int32_t *));
static int __log_get_lg_regionmax __P((DB_ENV *, u_int32_t *));

/*
 * __log_dbenv_create --
 *	Log specific initialization of the DB_ENV structure.
 *
 * PUBLIC: void __log_dbenv_create __P((DB_ENV *));
 */
void
__log_dbenv_create(dbenv)
	DB_ENV *dbenv;
{
	/*
	 * !!!
	 * Our caller has not yet had the opportunity to reset the panic
	 * state or turn off mutex locking, and so we can neither check
	 * the panic state or acquire a mutex in the DB_ENV create path.
	 */

	dbenv->lg_bsize = 0;
	dbenv->lg_regionmax = LG_BASE_REGION_SIZE;

#ifdef	HAVE_RPC
	/*
	 * If we have a client, overwrite what we just setup to
	 * point to client functions.
	 */
	if (F_ISSET(dbenv, DB_ENV_RPCCLIENT)) {
		dbenv->get_lg_bsize = __dbcl_get_lg_bsize;
		dbenv->set_lg_bsize = __dbcl_set_lg_bsize;
		dbenv->get_lg_dir = __dbcl_get_lg_dir;
		dbenv->set_lg_dir = __dbcl_set_lg_dir;
		dbenv->get_lg_max = __dbcl_get_lg_max;
		dbenv->set_lg_max = __dbcl_set_lg_max;
		dbenv->get_lg_regionmax = __dbcl_get_lg_regionmax;
		dbenv->set_lg_regionmax = __dbcl_set_lg_regionmax;

		dbenv->log_archive = __dbcl_log_archive;
		dbenv->log_cursor = __dbcl_log_cursor;
		dbenv->log_file = __dbcl_log_file;
		dbenv->log_flush = __dbcl_log_flush;
		dbenv->log_put = __dbcl_log_put;
		dbenv->log_stat = __dbcl_log_stat;
		dbenv->log_stat_print = NULL;
	} else
#endif
	{
		dbenv->get_lg_bsize = __log_get_lg_bsize;
		dbenv->set_lg_bsize = __log_set_lg_bsize;
		dbenv->get_lg_dir = __log_get_lg_dir;
		dbenv->set_lg_dir = __log_set_lg_dir;
		dbenv->get_lg_max = __log_get_lg_max;
		dbenv->set_lg_max = __log_set_lg_max;
		dbenv->get_lg_regionmax = __log_get_lg_regionmax;
		dbenv->set_lg_regionmax = __log_set_lg_regionmax;

		dbenv->log_archive = __log_archive_pp;
		dbenv->log_cursor = __log_cursor_pp;
		dbenv->log_file = __log_file_pp;
		dbenv->log_flush = __log_flush_pp;
		dbenv->log_put = __log_put_pp;
		dbenv->log_stat = __log_stat_pp;
		dbenv->log_stat_print = __log_stat_print_pp;
	}
}

static int
__log_get_lg_bsize(dbenv, lg_bsizep)
	DB_ENV *dbenv;
	u_int32_t *lg_bsizep;
{
	ENV_NOT_CONFIGURED(dbenv,
	    dbenv->lg_handle, "DB_ENV->get_lg_bsize", DB_INIT_LOG);

	if (LOGGING_ON(dbenv)) {
		/* Cannot be set after open, no lock required to read. */
		*lg_bsizep = ((LOG *)
		    ((DB_LOG *)dbenv->lg_handle)->reginfo.primary)->buffer_size;
	} else
		*lg_bsizep = dbenv->lg_bsize;
	return (0);
}

/*
 * __log_set_lg_bsize --
 *	DB_ENV->set_lg_bsize.
 *
 * PUBLIC: int __log_set_lg_bsize __P((DB_ENV *, u_int32_t));
 */
int
__log_set_lg_bsize(dbenv, lg_bsize)
	DB_ENV *dbenv;
	u_int32_t lg_bsize;
{
	ENV_ILLEGAL_AFTER_OPEN(dbenv, "DB_ENV->set_lg_bsize");

	dbenv->lg_bsize = lg_bsize;
	return (0);
}

static int
__log_get_lg_max(dbenv, lg_maxp)
	DB_ENV *dbenv;
	u_int32_t *lg_maxp;
{
	DB_LOG *dblp;

	ENV_NOT_CONFIGURED(dbenv,
	    dbenv->lg_handle, "DB_ENV->get_lg_max", DB_INIT_LOG);

	if (LOGGING_ON(dbenv)) {
		dblp = dbenv->lg_handle;
		R_LOCK(dbenv, &dblp->reginfo);
		*lg_maxp = ((LOG *)dblp->reginfo.primary)->log_nsize;
		R_UNLOCK(dbenv, &dblp->reginfo);
	} else
		*lg_maxp = dbenv->lg_size;

	return (0);
}

/*
 * __log_set_lg_max --
 *	DB_ENV->set_lg_max.
 *
 * PUBLIC: int __log_set_lg_max __P((DB_ENV *, u_int32_t));
 */
int
__log_set_lg_max(dbenv, lg_max)
	DB_ENV *dbenv;
	u_int32_t lg_max;
{
	DB_LOG *dblp;
	LOG *lp;
	int ret;

	ENV_NOT_CONFIGURED(dbenv,
	    dbenv->lg_handle, "DB_ENV->set_lg_max", DB_INIT_LOG);

	if (LOGGING_ON(dbenv)) {
		if ((ret = __log_check_sizes(dbenv, lg_max, 0)) != 0)
			return (ret);
		dblp = dbenv->lg_handle;
		lp = dblp->reginfo.primary;
		R_LOCK(dbenv, &dblp->reginfo);
		lp->log_nsize = lg_max;
		R_UNLOCK(dbenv, &dblp->reginfo);
	} else
		dbenv->lg_size = lg_max;

	return (0);
}

static int
__log_get_lg_regionmax(dbenv, lg_regionmaxp)
	DB_ENV *dbenv;
	u_int32_t *lg_regionmaxp;
{
	ENV_NOT_CONFIGURED(dbenv,
	    dbenv->lg_handle, "DB_ENV->get_lg_regionmax", DB_INIT_LOG);

	if (LOGGING_ON(dbenv)) {
		/* Cannot be set after open, no lock required to read. */
		*lg_regionmaxp = ((LOG *)
		    ((DB_LOG *)dbenv->lg_handle)->reginfo.primary)->regionmax;
	} else
		*lg_regionmaxp = dbenv->lg_regionmax;
	return (0);
}

/*
 * __log_set_lg_regionmax --
 *	DB_ENV->set_lg_regionmax.
 *
 * PUBLIC: int __log_set_lg_regionmax __P((DB_ENV *, u_int32_t));
 */
int
__log_set_lg_regionmax(dbenv, lg_regionmax)
	DB_ENV *dbenv;
	u_int32_t lg_regionmax;
{
	ENV_ILLEGAL_AFTER_OPEN(dbenv, "DB_ENV->set_lg_regionmax");

					/* Let's not be silly. */
	if (lg_regionmax != 0 && lg_regionmax < LG_BASE_REGION_SIZE) {
		__db_err(dbenv,
		    "log file size must be >= %d", LG_BASE_REGION_SIZE);
		return (EINVAL);
	}

	dbenv->lg_regionmax = lg_regionmax;
	return (0);
}

static int
__log_get_lg_dir(dbenv, dirp)
	DB_ENV *dbenv;
	const char **dirp;
{
	*dirp = dbenv->db_log_dir;
	return (0);
}

/*
 * __log_set_lg_dir --
 *	DB_ENV->set_lg_dir.
 *
 * PUBLIC: int __log_set_lg_dir __P((DB_ENV *, const char *));
 */
int
__log_set_lg_dir(dbenv, dir)
	DB_ENV *dbenv;
	const char *dir;
{
	if (dbenv->db_log_dir != NULL)
		__os_free(dbenv, dbenv->db_log_dir);
	return (__os_strdup(dbenv, dir, &dbenv->db_log_dir));
}

/*
 * __log_get_flags --
 *	DB_ENV->get_flags.
 *
 * PUBLIC: void __log_get_flags __P((DB_ENV *, u_int32_t *));
 */
void
__log_get_flags(dbenv, flagsp)
	DB_ENV *dbenv;
	u_int32_t *flagsp;
{
	DB_LOG *dblp;
	LOG *lp;
	u_int32_t flags;

	if ((dblp = dbenv->lg_handle) == NULL)
		return;

	lp = dblp->reginfo.primary;

	flags = *flagsp;
	if (lp->db_log_autoremove)
		LF_SET(DB_LOG_AUTOREMOVE);
	else
		LF_CLR(DB_LOG_AUTOREMOVE);
	if (lp->db_log_inmemory)
		LF_SET(DB_LOG_INMEMORY);
	else
		LF_CLR(DB_LOG_INMEMORY);
	*flagsp = flags;
}

/*
 * __log_set_flags --
 *	DB_ENV->set_flags.
 *
 * PUBLIC: void __log_set_flags __P((DB_ENV *, u_int32_t, int));
 */
void
__log_set_flags(dbenv, flags, on)
	DB_ENV *dbenv;
	u_int32_t flags;
	int on;
{
	DB_LOG *dblp;
	LOG *lp;

	if ((dblp = dbenv->lg_handle) == NULL)
		return;

	lp = dblp->reginfo.primary;

	if (LF_ISSET(DB_LOG_AUTOREMOVE))
		lp->db_log_autoremove = on ? 1 : 0;
	if (LF_ISSET(DB_LOG_INMEMORY))
		lp->db_log_inmemory = on ? 1 : 0;
}

/*
 * __log_check_sizes --
 *	Makes sure that the log file size and log buffer size are compatible.
 *
 * PUBLIC: int __log_check_sizes __P((DB_ENV *, u_int32_t, u_int32_t));
 */
int
__log_check_sizes(dbenv, lg_max, lg_bsize)
	DB_ENV *dbenv;
	u_int32_t lg_max;
	u_int32_t lg_bsize;
{
	LOG *lp;
	int inmem;

	if (LOGGING_ON(dbenv)) {
		lp = ((DB_LOG *)dbenv->lg_handle)->reginfo.primary;
		inmem = lp->db_log_inmemory;
		lg_bsize = lp->buffer_size;
	} else
		inmem = (F_ISSET(dbenv, DB_ENV_LOG_INMEMORY) != 0);

	if (inmem) {
		if (lg_bsize == 0)
			lg_bsize = LG_BSIZE_INMEM;
		if (lg_max == 0)
			lg_max = LG_MAX_INMEM;

		if (lg_bsize <= lg_max) {
			__db_err(dbenv,
		  "in-memory log buffer must be larger than the log file size");
			return (EINVAL);
		}
	}

	return (0);
}