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