1*7c478bd9Sstevel@tonic-gate /*-
2*7c478bd9Sstevel@tonic-gate * See the file LICENSE for redistribution information.
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * Copyright (c) 1996, 1997, 1998
5*7c478bd9Sstevel@tonic-gate * Sleepycat Software. All rights reserved.
6*7c478bd9Sstevel@tonic-gate */
7*7c478bd9Sstevel@tonic-gate #include "config.h"
8*7c478bd9Sstevel@tonic-gate
9*7c478bd9Sstevel@tonic-gate #ifndef lint
10*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)log.c 10.63 (Sleepycat) 10/10/98";
11*7c478bd9Sstevel@tonic-gate #endif /* not lint */
12*7c478bd9Sstevel@tonic-gate
13*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
14*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
15*7c478bd9Sstevel@tonic-gate
16*7c478bd9Sstevel@tonic-gate #include <errno.h>
17*7c478bd9Sstevel@tonic-gate #include <shqueue.h>
18*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
19*7c478bd9Sstevel@tonic-gate #include <string.h>
20*7c478bd9Sstevel@tonic-gate #include <unistd.h>
21*7c478bd9Sstevel@tonic-gate #endif
22*7c478bd9Sstevel@tonic-gate
23*7c478bd9Sstevel@tonic-gate #include "db_int.h"
24*7c478bd9Sstevel@tonic-gate #include "shqueue.h"
25*7c478bd9Sstevel@tonic-gate #include "log.h"
26*7c478bd9Sstevel@tonic-gate #include "db_dispatch.h"
27*7c478bd9Sstevel@tonic-gate #include "txn.h"
28*7c478bd9Sstevel@tonic-gate #include "txn_auto.h"
29*7c478bd9Sstevel@tonic-gate #include "common_ext.h"
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate static int __log_recover __P((DB_LOG *));
32*7c478bd9Sstevel@tonic-gate
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate * log_open --
35*7c478bd9Sstevel@tonic-gate * Initialize and/or join a log.
36*7c478bd9Sstevel@tonic-gate */
37*7c478bd9Sstevel@tonic-gate int
log_open(path,flags,mode,dbenv,lpp)38*7c478bd9Sstevel@tonic-gate log_open(path, flags, mode, dbenv, lpp)
39*7c478bd9Sstevel@tonic-gate const char *path;
40*7c478bd9Sstevel@tonic-gate u_int32_t flags;
41*7c478bd9Sstevel@tonic-gate int mode;
42*7c478bd9Sstevel@tonic-gate DB_ENV *dbenv;
43*7c478bd9Sstevel@tonic-gate DB_LOG **lpp;
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate DB_LOG *dblp;
46*7c478bd9Sstevel@tonic-gate LOG *lp;
47*7c478bd9Sstevel@tonic-gate int ret;
48*7c478bd9Sstevel@tonic-gate
49*7c478bd9Sstevel@tonic-gate /* Validate arguments. */
50*7c478bd9Sstevel@tonic-gate #ifdef HAVE_SPINLOCKS
51*7c478bd9Sstevel@tonic-gate #define OKFLAGS (DB_CREATE | DB_THREAD)
52*7c478bd9Sstevel@tonic-gate #else
53*7c478bd9Sstevel@tonic-gate #define OKFLAGS (DB_CREATE)
54*7c478bd9Sstevel@tonic-gate #endif
55*7c478bd9Sstevel@tonic-gate if ((ret = __db_fchk(dbenv, "log_open", flags, OKFLAGS)) != 0)
56*7c478bd9Sstevel@tonic-gate return (ret);
57*7c478bd9Sstevel@tonic-gate
58*7c478bd9Sstevel@tonic-gate /* Create and initialize the DB_LOG structure. */
59*7c478bd9Sstevel@tonic-gate if ((ret = __os_calloc(1, sizeof(DB_LOG), &dblp)) != 0)
60*7c478bd9Sstevel@tonic-gate return (ret);
61*7c478bd9Sstevel@tonic-gate
62*7c478bd9Sstevel@tonic-gate if (path != NULL && (ret = __os_strdup(path, &dblp->dir)) != 0)
63*7c478bd9Sstevel@tonic-gate goto err;
64*7c478bd9Sstevel@tonic-gate
65*7c478bd9Sstevel@tonic-gate dblp->dbenv = dbenv;
66*7c478bd9Sstevel@tonic-gate dblp->lfd = -1;
67*7c478bd9Sstevel@tonic-gate ZERO_LSN(dblp->c_lsn);
68*7c478bd9Sstevel@tonic-gate dblp->c_fd = -1;
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate /*
71*7c478bd9Sstevel@tonic-gate * The log region isn't fixed size because we store the registered
72*7c478bd9Sstevel@tonic-gate * file names there. Make it fairly large so that we don't have to
73*7c478bd9Sstevel@tonic-gate * grow it.
74*7c478bd9Sstevel@tonic-gate */
75*7c478bd9Sstevel@tonic-gate #define DEF_LOG_SIZE (30 * 1024)
76*7c478bd9Sstevel@tonic-gate
77*7c478bd9Sstevel@tonic-gate /* Map in the region. */
78*7c478bd9Sstevel@tonic-gate dblp->reginfo.dbenv = dbenv;
79*7c478bd9Sstevel@tonic-gate dblp->reginfo.appname = DB_APP_LOG;
80*7c478bd9Sstevel@tonic-gate if (path == NULL)
81*7c478bd9Sstevel@tonic-gate dblp->reginfo.path = NULL;
82*7c478bd9Sstevel@tonic-gate else
83*7c478bd9Sstevel@tonic-gate if ((ret = __os_strdup(path, &dblp->reginfo.path)) != 0)
84*7c478bd9Sstevel@tonic-gate goto err;
85*7c478bd9Sstevel@tonic-gate dblp->reginfo.file = DB_DEFAULT_LOG_FILE;
86*7c478bd9Sstevel@tonic-gate dblp->reginfo.mode = mode;
87*7c478bd9Sstevel@tonic-gate dblp->reginfo.size = DEF_LOG_SIZE;
88*7c478bd9Sstevel@tonic-gate dblp->reginfo.dbflags = flags;
89*7c478bd9Sstevel@tonic-gate dblp->reginfo.flags = REGION_SIZEDEF;
90*7c478bd9Sstevel@tonic-gate if ((ret = __db_rattach(&dblp->reginfo)) != 0)
91*7c478bd9Sstevel@tonic-gate goto err;
92*7c478bd9Sstevel@tonic-gate
93*7c478bd9Sstevel@tonic-gate /*
94*7c478bd9Sstevel@tonic-gate * The LOG structure is first in the region, the rest of the region
95*7c478bd9Sstevel@tonic-gate * is free space.
96*7c478bd9Sstevel@tonic-gate */
97*7c478bd9Sstevel@tonic-gate dblp->lp = dblp->reginfo.addr;
98*7c478bd9Sstevel@tonic-gate dblp->addr = (u_int8_t *)dblp->lp + sizeof(LOG);
99*7c478bd9Sstevel@tonic-gate
100*7c478bd9Sstevel@tonic-gate /* Initialize a created region. */
101*7c478bd9Sstevel@tonic-gate if (F_ISSET(&dblp->reginfo, REGION_CREATED)) {
102*7c478bd9Sstevel@tonic-gate __db_shalloc_init(dblp->addr, DEF_LOG_SIZE - sizeof(LOG));
103*7c478bd9Sstevel@tonic-gate
104*7c478bd9Sstevel@tonic-gate /* Initialize the LOG structure. */
105*7c478bd9Sstevel@tonic-gate lp = dblp->lp;
106*7c478bd9Sstevel@tonic-gate lp->persist.lg_max = dbenv == NULL ? 0 : dbenv->lg_max;
107*7c478bd9Sstevel@tonic-gate if (lp->persist.lg_max == 0)
108*7c478bd9Sstevel@tonic-gate lp->persist.lg_max = DEFAULT_MAX;
109*7c478bd9Sstevel@tonic-gate lp->persist.magic = DB_LOGMAGIC;
110*7c478bd9Sstevel@tonic-gate lp->persist.version = DB_LOGVERSION;
111*7c478bd9Sstevel@tonic-gate lp->persist.mode = mode;
112*7c478bd9Sstevel@tonic-gate SH_TAILQ_INIT(&lp->fq);
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate /* Initialize LOG LSNs. */
115*7c478bd9Sstevel@tonic-gate lp->lsn.file = 1;
116*7c478bd9Sstevel@tonic-gate lp->lsn.offset = 0;
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate
119*7c478bd9Sstevel@tonic-gate /* Initialize thread information, mutex. */
120*7c478bd9Sstevel@tonic-gate if (LF_ISSET(DB_THREAD)) {
121*7c478bd9Sstevel@tonic-gate F_SET(dblp, DB_AM_THREAD);
122*7c478bd9Sstevel@tonic-gate if ((ret = __db_shalloc(dblp->addr,
123*7c478bd9Sstevel@tonic-gate sizeof(db_mutex_t), MUTEX_ALIGNMENT, &dblp->mutexp)) != 0)
124*7c478bd9Sstevel@tonic-gate goto err;
125*7c478bd9Sstevel@tonic-gate (void)__db_mutex_init(dblp->mutexp, 0);
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate * If doing recovery, try and recover any previous log files before
130*7c478bd9Sstevel@tonic-gate * releasing the lock.
131*7c478bd9Sstevel@tonic-gate */
132*7c478bd9Sstevel@tonic-gate if (F_ISSET(&dblp->reginfo, REGION_CREATED) &&
133*7c478bd9Sstevel@tonic-gate (ret = __log_recover(dblp)) != 0)
134*7c478bd9Sstevel@tonic-gate goto err;
135*7c478bd9Sstevel@tonic-gate
136*7c478bd9Sstevel@tonic-gate UNLOCK_LOGREGION(dblp);
137*7c478bd9Sstevel@tonic-gate *lpp = dblp;
138*7c478bd9Sstevel@tonic-gate return (0);
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate err: if (dblp->reginfo.addr != NULL) {
141*7c478bd9Sstevel@tonic-gate if (dblp->mutexp != NULL)
142*7c478bd9Sstevel@tonic-gate __db_shalloc_free(dblp->addr, dblp->mutexp);
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate UNLOCK_LOGREGION(dblp);
145*7c478bd9Sstevel@tonic-gate (void)__db_rdetach(&dblp->reginfo);
146*7c478bd9Sstevel@tonic-gate if (F_ISSET(&dblp->reginfo, REGION_CREATED))
147*7c478bd9Sstevel@tonic-gate (void)log_unlink(path, 1, dbenv);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate if (dblp->reginfo.path != NULL)
151*7c478bd9Sstevel@tonic-gate __os_freestr(dblp->reginfo.path);
152*7c478bd9Sstevel@tonic-gate if (dblp->dir != NULL)
153*7c478bd9Sstevel@tonic-gate __os_freestr(dblp->dir);
154*7c478bd9Sstevel@tonic-gate __os_free(dblp, sizeof(*dblp));
155*7c478bd9Sstevel@tonic-gate return (ret);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate /*
159*7c478bd9Sstevel@tonic-gate * __log_panic --
160*7c478bd9Sstevel@tonic-gate * Panic a log.
161*7c478bd9Sstevel@tonic-gate *
162*7c478bd9Sstevel@tonic-gate * PUBLIC: void __log_panic __P((DB_ENV *));
163*7c478bd9Sstevel@tonic-gate */
164*7c478bd9Sstevel@tonic-gate void
__log_panic(dbenv)165*7c478bd9Sstevel@tonic-gate __log_panic(dbenv)
166*7c478bd9Sstevel@tonic-gate DB_ENV *dbenv;
167*7c478bd9Sstevel@tonic-gate {
168*7c478bd9Sstevel@tonic-gate if (dbenv->lg_info != NULL)
169*7c478bd9Sstevel@tonic-gate dbenv->lg_info->lp->rlayout.panic = 1;
170*7c478bd9Sstevel@tonic-gate }
171*7c478bd9Sstevel@tonic-gate
172*7c478bd9Sstevel@tonic-gate /*
173*7c478bd9Sstevel@tonic-gate * __log_recover --
174*7c478bd9Sstevel@tonic-gate * Recover a log.
175*7c478bd9Sstevel@tonic-gate */
176*7c478bd9Sstevel@tonic-gate static int
__log_recover(dblp)177*7c478bd9Sstevel@tonic-gate __log_recover(dblp)
178*7c478bd9Sstevel@tonic-gate DB_LOG *dblp;
179*7c478bd9Sstevel@tonic-gate {
180*7c478bd9Sstevel@tonic-gate DBT dbt;
181*7c478bd9Sstevel@tonic-gate DB_LSN lsn;
182*7c478bd9Sstevel@tonic-gate LOG *lp;
183*7c478bd9Sstevel@tonic-gate u_int32_t chk;
184*7c478bd9Sstevel@tonic-gate int cnt, found_checkpoint, ret;
185*7c478bd9Sstevel@tonic-gate
186*7c478bd9Sstevel@tonic-gate lp = dblp->lp;
187*7c478bd9Sstevel@tonic-gate
188*7c478bd9Sstevel@tonic-gate /*
189*7c478bd9Sstevel@tonic-gate * Find a log file. If none exist, we simply return, leaving
190*7c478bd9Sstevel@tonic-gate * everything initialized to a new log.
191*7c478bd9Sstevel@tonic-gate */
192*7c478bd9Sstevel@tonic-gate if ((ret = __log_find(dblp, 0, &cnt)) != 0)
193*7c478bd9Sstevel@tonic-gate return (ret);
194*7c478bd9Sstevel@tonic-gate if (cnt == 0)
195*7c478bd9Sstevel@tonic-gate return (0);
196*7c478bd9Sstevel@tonic-gate
197*7c478bd9Sstevel@tonic-gate /*
198*7c478bd9Sstevel@tonic-gate * We have the last useful log file and we've loaded any persistent
199*7c478bd9Sstevel@tonic-gate * information. Pretend that the log is larger than it can possibly
200*7c478bd9Sstevel@tonic-gate * be, and read the last file, looking for the last checkpoint and
201*7c478bd9Sstevel@tonic-gate * the log's end.
202*7c478bd9Sstevel@tonic-gate */
203*7c478bd9Sstevel@tonic-gate lp->lsn.file = cnt + 1;
204*7c478bd9Sstevel@tonic-gate lp->lsn.offset = 0;
205*7c478bd9Sstevel@tonic-gate lsn.file = cnt;
206*7c478bd9Sstevel@tonic-gate lsn.offset = 0;
207*7c478bd9Sstevel@tonic-gate
208*7c478bd9Sstevel@tonic-gate /* Set the cursor. Shouldn't fail, leave error messages on. */
209*7c478bd9Sstevel@tonic-gate memset(&dbt, 0, sizeof(dbt));
210*7c478bd9Sstevel@tonic-gate if ((ret = __log_get(dblp, &lsn, &dbt, DB_SET, 0)) != 0)
211*7c478bd9Sstevel@tonic-gate return (ret);
212*7c478bd9Sstevel@tonic-gate
213*7c478bd9Sstevel@tonic-gate /*
214*7c478bd9Sstevel@tonic-gate * Read to the end of the file, saving checkpoints. This will fail
215*7c478bd9Sstevel@tonic-gate * at some point, so turn off error messages.
216*7c478bd9Sstevel@tonic-gate */
217*7c478bd9Sstevel@tonic-gate found_checkpoint = 0;
218*7c478bd9Sstevel@tonic-gate while (__log_get(dblp, &lsn, &dbt, DB_NEXT, 1) == 0) {
219*7c478bd9Sstevel@tonic-gate if (dbt.size < sizeof(u_int32_t))
220*7c478bd9Sstevel@tonic-gate continue;
221*7c478bd9Sstevel@tonic-gate memcpy(&chk, dbt.data, sizeof(u_int32_t));
222*7c478bd9Sstevel@tonic-gate if (chk == DB_txn_ckp) {
223*7c478bd9Sstevel@tonic-gate lp->chkpt_lsn = lsn;
224*7c478bd9Sstevel@tonic-gate found_checkpoint = 1;
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate
228*7c478bd9Sstevel@tonic-gate /*
229*7c478bd9Sstevel@tonic-gate * We now know where the end of the log is. Set the first LSN that
230*7c478bd9Sstevel@tonic-gate * we want to return to an application and the LSN of the last known
231*7c478bd9Sstevel@tonic-gate * record on disk.
232*7c478bd9Sstevel@tonic-gate */
233*7c478bd9Sstevel@tonic-gate lp->lsn = lp->s_lsn = lsn;
234*7c478bd9Sstevel@tonic-gate lp->lsn.offset += dblp->c_len;
235*7c478bd9Sstevel@tonic-gate
236*7c478bd9Sstevel@tonic-gate /* Set up the current buffer information, too. */
237*7c478bd9Sstevel@tonic-gate lp->len = dblp->c_len;
238*7c478bd9Sstevel@tonic-gate lp->b_off = 0;
239*7c478bd9Sstevel@tonic-gate lp->w_off = lp->lsn.offset;
240*7c478bd9Sstevel@tonic-gate
241*7c478bd9Sstevel@tonic-gate /*
242*7c478bd9Sstevel@tonic-gate * It's possible that we didn't find a checkpoint because there wasn't
243*7c478bd9Sstevel@tonic-gate * one in the last log file. Start searching.
244*7c478bd9Sstevel@tonic-gate */
245*7c478bd9Sstevel@tonic-gate while (!found_checkpoint && cnt > 1) {
246*7c478bd9Sstevel@tonic-gate lsn.file = --cnt;
247*7c478bd9Sstevel@tonic-gate lsn.offset = 0;
248*7c478bd9Sstevel@tonic-gate
249*7c478bd9Sstevel@tonic-gate /* Set the cursor. Shouldn't fail, leave error messages on. */
250*7c478bd9Sstevel@tonic-gate if ((ret = __log_get(dblp, &lsn, &dbt, DB_SET, 0)) != 0)
251*7c478bd9Sstevel@tonic-gate return (ret);
252*7c478bd9Sstevel@tonic-gate
253*7c478bd9Sstevel@tonic-gate /*
254*7c478bd9Sstevel@tonic-gate * Read to the end of the file, saving checkpoints. Shouldn't
255*7c478bd9Sstevel@tonic-gate * fail, leave error messages on.
256*7c478bd9Sstevel@tonic-gate */
257*7c478bd9Sstevel@tonic-gate while (__log_get(dblp, &lsn, &dbt, DB_NEXT, 0) == 0) {
258*7c478bd9Sstevel@tonic-gate if (dbt.size < sizeof(u_int32_t))
259*7c478bd9Sstevel@tonic-gate continue;
260*7c478bd9Sstevel@tonic-gate memcpy(&chk, dbt.data, sizeof(u_int32_t));
261*7c478bd9Sstevel@tonic-gate if (chk == DB_txn_ckp) {
262*7c478bd9Sstevel@tonic-gate lp->chkpt_lsn = lsn;
263*7c478bd9Sstevel@tonic-gate found_checkpoint = 1;
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate }
266*7c478bd9Sstevel@tonic-gate }
267*7c478bd9Sstevel@tonic-gate /*
268*7c478bd9Sstevel@tonic-gate * Reset the cursor lsn to the beginning of the log, so that an
269*7c478bd9Sstevel@tonic-gate * initial call to DB_NEXT does the right thing.
270*7c478bd9Sstevel@tonic-gate */
271*7c478bd9Sstevel@tonic-gate ZERO_LSN(dblp->c_lsn);
272*7c478bd9Sstevel@tonic-gate
273*7c478bd9Sstevel@tonic-gate /* If we never find a checkpoint, that's okay, just 0 it out. */
274*7c478bd9Sstevel@tonic-gate if (!found_checkpoint)
275*7c478bd9Sstevel@tonic-gate ZERO_LSN(lp->chkpt_lsn);
276*7c478bd9Sstevel@tonic-gate
277*7c478bd9Sstevel@tonic-gate /*
278*7c478bd9Sstevel@tonic-gate * !!!
279*7c478bd9Sstevel@tonic-gate * The test suite explicitly looks for this string -- don't change
280*7c478bd9Sstevel@tonic-gate * it here unless you also change it there.
281*7c478bd9Sstevel@tonic-gate */
282*7c478bd9Sstevel@tonic-gate __db_err(dblp->dbenv,
283*7c478bd9Sstevel@tonic-gate "Finding last valid log LSN: file: %lu offset %lu",
284*7c478bd9Sstevel@tonic-gate (u_long)lp->lsn.file, (u_long)lp->lsn.offset);
285*7c478bd9Sstevel@tonic-gate
286*7c478bd9Sstevel@tonic-gate return (0);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate
289*7c478bd9Sstevel@tonic-gate /*
290*7c478bd9Sstevel@tonic-gate * __log_find --
291*7c478bd9Sstevel@tonic-gate * Try to find a log file. If find_first is set, valp will contain
292*7c478bd9Sstevel@tonic-gate * the number of the first log file, else it will contain the number of
293*7c478bd9Sstevel@tonic-gate * the last log file.
294*7c478bd9Sstevel@tonic-gate *
295*7c478bd9Sstevel@tonic-gate * PUBLIC: int __log_find __P((DB_LOG *, int, int *));
296*7c478bd9Sstevel@tonic-gate */
297*7c478bd9Sstevel@tonic-gate int
__log_find(dblp,find_first,valp)298*7c478bd9Sstevel@tonic-gate __log_find(dblp, find_first, valp)
299*7c478bd9Sstevel@tonic-gate DB_LOG *dblp;
300*7c478bd9Sstevel@tonic-gate int find_first, *valp;
301*7c478bd9Sstevel@tonic-gate {
302*7c478bd9Sstevel@tonic-gate u_int32_t clv, logval;
303*7c478bd9Sstevel@tonic-gate int cnt, fcnt, ret;
304*7c478bd9Sstevel@tonic-gate const char *dir;
305*7c478bd9Sstevel@tonic-gate char **names, *p, *q;
306*7c478bd9Sstevel@tonic-gate
307*7c478bd9Sstevel@tonic-gate *valp = 0;
308*7c478bd9Sstevel@tonic-gate
309*7c478bd9Sstevel@tonic-gate /* Find the directory name. */
310*7c478bd9Sstevel@tonic-gate if ((ret = __log_name(dblp, 1, &p, NULL, 0)) != 0)
311*7c478bd9Sstevel@tonic-gate return (ret);
312*7c478bd9Sstevel@tonic-gate if ((q = __db_rpath(p)) == NULL)
313*7c478bd9Sstevel@tonic-gate dir = PATH_DOT;
314*7c478bd9Sstevel@tonic-gate else {
315*7c478bd9Sstevel@tonic-gate *q = '\0';
316*7c478bd9Sstevel@tonic-gate dir = p;
317*7c478bd9Sstevel@tonic-gate }
318*7c478bd9Sstevel@tonic-gate
319*7c478bd9Sstevel@tonic-gate /* Get the list of file names. */
320*7c478bd9Sstevel@tonic-gate ret = __os_dirlist(dir, &names, &fcnt);
321*7c478bd9Sstevel@tonic-gate __os_freestr(p);
322*7c478bd9Sstevel@tonic-gate if (ret != 0) {
323*7c478bd9Sstevel@tonic-gate __db_err(dblp->dbenv, "%s: %s", dir, strerror(ret));
324*7c478bd9Sstevel@tonic-gate return (ret);
325*7c478bd9Sstevel@tonic-gate }
326*7c478bd9Sstevel@tonic-gate
327*7c478bd9Sstevel@tonic-gate /*
328*7c478bd9Sstevel@tonic-gate * Search for a valid log file name, return a value of 0 on
329*7c478bd9Sstevel@tonic-gate * failure.
330*7c478bd9Sstevel@tonic-gate *
331*7c478bd9Sstevel@tonic-gate * XXX
332*7c478bd9Sstevel@tonic-gate * Assumes that atoi(3) returns a 32-bit number.
333*7c478bd9Sstevel@tonic-gate */
334*7c478bd9Sstevel@tonic-gate for (cnt = fcnt, clv = logval = 0; --cnt >= 0;) {
335*7c478bd9Sstevel@tonic-gate if (strncmp(names[cnt], LFPREFIX, sizeof(LFPREFIX) - 1) != 0)
336*7c478bd9Sstevel@tonic-gate continue;
337*7c478bd9Sstevel@tonic-gate
338*7c478bd9Sstevel@tonic-gate clv = atoi(names[cnt] + (sizeof(LFPREFIX) - 1));
339*7c478bd9Sstevel@tonic-gate if (find_first) {
340*7c478bd9Sstevel@tonic-gate if (logval != 0 && clv > logval)
341*7c478bd9Sstevel@tonic-gate continue;
342*7c478bd9Sstevel@tonic-gate } else
343*7c478bd9Sstevel@tonic-gate if (logval != 0 && clv < logval)
344*7c478bd9Sstevel@tonic-gate continue;
345*7c478bd9Sstevel@tonic-gate
346*7c478bd9Sstevel@tonic-gate if (__log_valid(dblp, clv, 1) == 0)
347*7c478bd9Sstevel@tonic-gate logval = clv;
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate
350*7c478bd9Sstevel@tonic-gate *valp = logval;
351*7c478bd9Sstevel@tonic-gate
352*7c478bd9Sstevel@tonic-gate /* Discard the list. */
353*7c478bd9Sstevel@tonic-gate __os_dirfree(names, fcnt);
354*7c478bd9Sstevel@tonic-gate
355*7c478bd9Sstevel@tonic-gate return (0);
356*7c478bd9Sstevel@tonic-gate }
357*7c478bd9Sstevel@tonic-gate
358*7c478bd9Sstevel@tonic-gate /*
359*7c478bd9Sstevel@tonic-gate * log_valid --
360*7c478bd9Sstevel@tonic-gate * Validate a log file.
361*7c478bd9Sstevel@tonic-gate *
362*7c478bd9Sstevel@tonic-gate * PUBLIC: int __log_valid __P((DB_LOG *, u_int32_t, int));
363*7c478bd9Sstevel@tonic-gate */
364*7c478bd9Sstevel@tonic-gate int
__log_valid(dblp,number,set_persist)365*7c478bd9Sstevel@tonic-gate __log_valid(dblp, number, set_persist)
366*7c478bd9Sstevel@tonic-gate DB_LOG *dblp;
367*7c478bd9Sstevel@tonic-gate u_int32_t number;
368*7c478bd9Sstevel@tonic-gate int set_persist;
369*7c478bd9Sstevel@tonic-gate {
370*7c478bd9Sstevel@tonic-gate LOGP persist;
371*7c478bd9Sstevel@tonic-gate ssize_t nw;
372*7c478bd9Sstevel@tonic-gate char *fname;
373*7c478bd9Sstevel@tonic-gate int fd, ret;
374*7c478bd9Sstevel@tonic-gate
375*7c478bd9Sstevel@tonic-gate /* Try to open the log file. */
376*7c478bd9Sstevel@tonic-gate if ((ret = __log_name(dblp,
377*7c478bd9Sstevel@tonic-gate number, &fname, &fd, DB_RDONLY | DB_SEQUENTIAL)) != 0) {
378*7c478bd9Sstevel@tonic-gate __os_freestr(fname);
379*7c478bd9Sstevel@tonic-gate return (ret);
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate
382*7c478bd9Sstevel@tonic-gate /* Try to read the header. */
383*7c478bd9Sstevel@tonic-gate if ((ret = __os_seek(fd, 0, 0, sizeof(HDR), 0, SEEK_SET)) != 0 ||
384*7c478bd9Sstevel@tonic-gate (ret = __os_read(fd, &persist, sizeof(LOGP), &nw)) != 0 ||
385*7c478bd9Sstevel@tonic-gate nw != sizeof(LOGP)) {
386*7c478bd9Sstevel@tonic-gate if (ret == 0)
387*7c478bd9Sstevel@tonic-gate ret = EIO;
388*7c478bd9Sstevel@tonic-gate
389*7c478bd9Sstevel@tonic-gate (void)__os_close(fd);
390*7c478bd9Sstevel@tonic-gate
391*7c478bd9Sstevel@tonic-gate __db_err(dblp->dbenv,
392*7c478bd9Sstevel@tonic-gate "Ignoring log file: %s: %s", fname, strerror(ret));
393*7c478bd9Sstevel@tonic-gate goto err;
394*7c478bd9Sstevel@tonic-gate }
395*7c478bd9Sstevel@tonic-gate (void)__os_close(fd);
396*7c478bd9Sstevel@tonic-gate
397*7c478bd9Sstevel@tonic-gate /* Validate the header. */
398*7c478bd9Sstevel@tonic-gate if (persist.magic != DB_LOGMAGIC) {
399*7c478bd9Sstevel@tonic-gate __db_err(dblp->dbenv,
400*7c478bd9Sstevel@tonic-gate "Ignoring log file: %s: magic number %lx, not %lx",
401*7c478bd9Sstevel@tonic-gate fname, (u_long)persist.magic, (u_long)DB_LOGMAGIC);
402*7c478bd9Sstevel@tonic-gate ret = EINVAL;
403*7c478bd9Sstevel@tonic-gate goto err;
404*7c478bd9Sstevel@tonic-gate }
405*7c478bd9Sstevel@tonic-gate if (persist.version < DB_LOGOLDVER || persist.version > DB_LOGVERSION) {
406*7c478bd9Sstevel@tonic-gate __db_err(dblp->dbenv,
407*7c478bd9Sstevel@tonic-gate "Ignoring log file: %s: unsupported log version %lu",
408*7c478bd9Sstevel@tonic-gate fname, (u_long)persist.version);
409*7c478bd9Sstevel@tonic-gate ret = EINVAL;
410*7c478bd9Sstevel@tonic-gate goto err;
411*7c478bd9Sstevel@tonic-gate }
412*7c478bd9Sstevel@tonic-gate
413*7c478bd9Sstevel@tonic-gate /*
414*7c478bd9Sstevel@tonic-gate * If we're going to use this log file, set the region's persistent
415*7c478bd9Sstevel@tonic-gate * information based on the headers.
416*7c478bd9Sstevel@tonic-gate */
417*7c478bd9Sstevel@tonic-gate if (set_persist) {
418*7c478bd9Sstevel@tonic-gate dblp->lp->persist.lg_max = persist.lg_max;
419*7c478bd9Sstevel@tonic-gate dblp->lp->persist.mode = persist.mode;
420*7c478bd9Sstevel@tonic-gate }
421*7c478bd9Sstevel@tonic-gate ret = 0;
422*7c478bd9Sstevel@tonic-gate
423*7c478bd9Sstevel@tonic-gate err: __os_freestr(fname);
424*7c478bd9Sstevel@tonic-gate return (ret);
425*7c478bd9Sstevel@tonic-gate }
426*7c478bd9Sstevel@tonic-gate
427*7c478bd9Sstevel@tonic-gate /*
428*7c478bd9Sstevel@tonic-gate * log_close --
429*7c478bd9Sstevel@tonic-gate * Close a log.
430*7c478bd9Sstevel@tonic-gate */
431*7c478bd9Sstevel@tonic-gate int
log_close(dblp)432*7c478bd9Sstevel@tonic-gate log_close(dblp)
433*7c478bd9Sstevel@tonic-gate DB_LOG *dblp;
434*7c478bd9Sstevel@tonic-gate {
435*7c478bd9Sstevel@tonic-gate u_int32_t i;
436*7c478bd9Sstevel@tonic-gate int ret, t_ret;
437*7c478bd9Sstevel@tonic-gate
438*7c478bd9Sstevel@tonic-gate LOG_PANIC_CHECK(dblp);
439*7c478bd9Sstevel@tonic-gate
440*7c478bd9Sstevel@tonic-gate /* We may have opened files as part of XA; if so, close them. */
441*7c478bd9Sstevel@tonic-gate __log_close_files(dblp);
442*7c478bd9Sstevel@tonic-gate
443*7c478bd9Sstevel@tonic-gate /* Discard the per-thread pointer. */
444*7c478bd9Sstevel@tonic-gate if (dblp->mutexp != NULL) {
445*7c478bd9Sstevel@tonic-gate LOCK_LOGREGION(dblp);
446*7c478bd9Sstevel@tonic-gate __db_shalloc_free(dblp->addr, dblp->mutexp);
447*7c478bd9Sstevel@tonic-gate UNLOCK_LOGREGION(dblp);
448*7c478bd9Sstevel@tonic-gate }
449*7c478bd9Sstevel@tonic-gate
450*7c478bd9Sstevel@tonic-gate /* Close the region. */
451*7c478bd9Sstevel@tonic-gate ret = __db_rdetach(&dblp->reginfo);
452*7c478bd9Sstevel@tonic-gate
453*7c478bd9Sstevel@tonic-gate /* Close open files, release allocated memory. */
454*7c478bd9Sstevel@tonic-gate if (dblp->lfd != -1 && (t_ret = __os_close(dblp->lfd)) != 0 && ret == 0)
455*7c478bd9Sstevel@tonic-gate ret = t_ret;
456*7c478bd9Sstevel@tonic-gate if (dblp->c_dbt.data != NULL)
457*7c478bd9Sstevel@tonic-gate __os_free(dblp->c_dbt.data, dblp->c_dbt.ulen);
458*7c478bd9Sstevel@tonic-gate if (dblp->c_fd != -1 &&
459*7c478bd9Sstevel@tonic-gate (t_ret = __os_close(dblp->c_fd)) != 0 && ret == 0)
460*7c478bd9Sstevel@tonic-gate ret = t_ret;
461*7c478bd9Sstevel@tonic-gate if (dblp->dbentry != NULL) {
462*7c478bd9Sstevel@tonic-gate for (i = 0; i < dblp->dbentry_cnt; i++)
463*7c478bd9Sstevel@tonic-gate if (dblp->dbentry[i].name != NULL)
464*7c478bd9Sstevel@tonic-gate __os_freestr(dblp->dbentry[i].name);
465*7c478bd9Sstevel@tonic-gate __os_free(dblp->dbentry,
466*7c478bd9Sstevel@tonic-gate (dblp->dbentry_cnt * sizeof(DB_ENTRY)));
467*7c478bd9Sstevel@tonic-gate }
468*7c478bd9Sstevel@tonic-gate
469*7c478bd9Sstevel@tonic-gate if (dblp->dir != NULL)
470*7c478bd9Sstevel@tonic-gate __os_freestr(dblp->dir);
471*7c478bd9Sstevel@tonic-gate
472*7c478bd9Sstevel@tonic-gate if (dblp->reginfo.path != NULL)
473*7c478bd9Sstevel@tonic-gate __os_freestr(dblp->reginfo.path);
474*7c478bd9Sstevel@tonic-gate __os_free(dblp, sizeof(*dblp));
475*7c478bd9Sstevel@tonic-gate
476*7c478bd9Sstevel@tonic-gate return (ret);
477*7c478bd9Sstevel@tonic-gate }
478*7c478bd9Sstevel@tonic-gate
479*7c478bd9Sstevel@tonic-gate /*
480*7c478bd9Sstevel@tonic-gate * log_unlink --
481*7c478bd9Sstevel@tonic-gate * Exit a log.
482*7c478bd9Sstevel@tonic-gate */
483*7c478bd9Sstevel@tonic-gate int
log_unlink(path,force,dbenv)484*7c478bd9Sstevel@tonic-gate log_unlink(path, force, dbenv)
485*7c478bd9Sstevel@tonic-gate const char *path;
486*7c478bd9Sstevel@tonic-gate int force;
487*7c478bd9Sstevel@tonic-gate DB_ENV *dbenv;
488*7c478bd9Sstevel@tonic-gate {
489*7c478bd9Sstevel@tonic-gate REGINFO reginfo;
490*7c478bd9Sstevel@tonic-gate int ret;
491*7c478bd9Sstevel@tonic-gate
492*7c478bd9Sstevel@tonic-gate memset(®info, 0, sizeof(reginfo));
493*7c478bd9Sstevel@tonic-gate reginfo.dbenv = dbenv;
494*7c478bd9Sstevel@tonic-gate reginfo.appname = DB_APP_LOG;
495*7c478bd9Sstevel@tonic-gate if (path != NULL && (ret = __os_strdup(path, ®info.path)) != 0)
496*7c478bd9Sstevel@tonic-gate return (ret);
497*7c478bd9Sstevel@tonic-gate reginfo.file = DB_DEFAULT_LOG_FILE;
498*7c478bd9Sstevel@tonic-gate ret = __db_runlink(®info, force);
499*7c478bd9Sstevel@tonic-gate if (reginfo.path != NULL)
500*7c478bd9Sstevel@tonic-gate __os_freestr(reginfo.path);
501*7c478bd9Sstevel@tonic-gate return (ret);
502*7c478bd9Sstevel@tonic-gate }
503*7c478bd9Sstevel@tonic-gate
504*7c478bd9Sstevel@tonic-gate /*
505*7c478bd9Sstevel@tonic-gate * log_stat --
506*7c478bd9Sstevel@tonic-gate * Return LOG statistics.
507*7c478bd9Sstevel@tonic-gate */
508*7c478bd9Sstevel@tonic-gate int
log_stat(dblp,gspp,db_malloc)509*7c478bd9Sstevel@tonic-gate log_stat(dblp, gspp, db_malloc)
510*7c478bd9Sstevel@tonic-gate DB_LOG *dblp;
511*7c478bd9Sstevel@tonic-gate DB_LOG_STAT **gspp;
512*7c478bd9Sstevel@tonic-gate void *(*db_malloc) __P((size_t));
513*7c478bd9Sstevel@tonic-gate {
514*7c478bd9Sstevel@tonic-gate LOG *lp;
515*7c478bd9Sstevel@tonic-gate int ret;
516*7c478bd9Sstevel@tonic-gate
517*7c478bd9Sstevel@tonic-gate *gspp = NULL;
518*7c478bd9Sstevel@tonic-gate lp = dblp->lp;
519*7c478bd9Sstevel@tonic-gate
520*7c478bd9Sstevel@tonic-gate LOG_PANIC_CHECK(dblp);
521*7c478bd9Sstevel@tonic-gate
522*7c478bd9Sstevel@tonic-gate if ((ret = __os_malloc(sizeof(**gspp), db_malloc, gspp)) != 0)
523*7c478bd9Sstevel@tonic-gate return (ret);
524*7c478bd9Sstevel@tonic-gate
525*7c478bd9Sstevel@tonic-gate /* Copy out the global statistics. */
526*7c478bd9Sstevel@tonic-gate LOCK_LOGREGION(dblp);
527*7c478bd9Sstevel@tonic-gate **gspp = lp->stat;
528*7c478bd9Sstevel@tonic-gate
529*7c478bd9Sstevel@tonic-gate (*gspp)->st_magic = lp->persist.magic;
530*7c478bd9Sstevel@tonic-gate (*gspp)->st_version = lp->persist.version;
531*7c478bd9Sstevel@tonic-gate (*gspp)->st_mode = lp->persist.mode;
532*7c478bd9Sstevel@tonic-gate (*gspp)->st_lg_max = lp->persist.lg_max;
533*7c478bd9Sstevel@tonic-gate
534*7c478bd9Sstevel@tonic-gate (*gspp)->st_region_nowait = lp->rlayout.lock.mutex_set_nowait;
535*7c478bd9Sstevel@tonic-gate (*gspp)->st_region_wait = lp->rlayout.lock.mutex_set_wait;
536*7c478bd9Sstevel@tonic-gate
537*7c478bd9Sstevel@tonic-gate (*gspp)->st_cur_file = lp->lsn.file;
538*7c478bd9Sstevel@tonic-gate (*gspp)->st_cur_offset = lp->lsn.offset;
539*7c478bd9Sstevel@tonic-gate
540*7c478bd9Sstevel@tonic-gate (*gspp)->st_refcnt = lp->rlayout.refcnt;
541*7c478bd9Sstevel@tonic-gate (*gspp)->st_regsize = lp->rlayout.size;
542*7c478bd9Sstevel@tonic-gate
543*7c478bd9Sstevel@tonic-gate UNLOCK_LOGREGION(dblp);
544*7c478bd9Sstevel@tonic-gate
545*7c478bd9Sstevel@tonic-gate return (0);
546*7c478bd9Sstevel@tonic-gate }
547