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_put.c 10.44 (Sleepycat) 11/3/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 <stdio.h> 18*0Sstevel@tonic-gate #include <string.h> 19*0Sstevel@tonic-gate #include <time.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 "db_page.h" 26*0Sstevel@tonic-gate #include "log.h" 27*0Sstevel@tonic-gate #include "hash.h" 28*0Sstevel@tonic-gate #include "clib_ext.h" 29*0Sstevel@tonic-gate #include "common_ext.h" 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate static int __log_fill __P((DB_LOG *, DB_LSN *, void *, u_int32_t)); 32*0Sstevel@tonic-gate static int __log_flush __P((DB_LOG *, const DB_LSN *)); 33*0Sstevel@tonic-gate static int __log_newfd __P((DB_LOG *)); 34*0Sstevel@tonic-gate static int __log_putr __P((DB_LOG *, DB_LSN *, const DBT *, u_int32_t)); 35*0Sstevel@tonic-gate static int __log_write __P((DB_LOG *, void *, u_int32_t)); 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate /* 38*0Sstevel@tonic-gate * log_put -- 39*0Sstevel@tonic-gate * Write a log record. 40*0Sstevel@tonic-gate */ 41*0Sstevel@tonic-gate int 42*0Sstevel@tonic-gate log_put(dblp, lsn, dbt, flags) 43*0Sstevel@tonic-gate DB_LOG *dblp; 44*0Sstevel@tonic-gate DB_LSN *lsn; 45*0Sstevel@tonic-gate const DBT *dbt; 46*0Sstevel@tonic-gate u_int32_t flags; 47*0Sstevel@tonic-gate { 48*0Sstevel@tonic-gate int ret; 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate LOG_PANIC_CHECK(dblp); 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate /* Validate arguments. */ 53*0Sstevel@tonic-gate if (flags != 0 && flags != DB_CHECKPOINT && 54*0Sstevel@tonic-gate flags != DB_CURLSN && flags != DB_FLUSH) 55*0Sstevel@tonic-gate return (__db_ferr(dblp->dbenv, "log_put", 0)); 56*0Sstevel@tonic-gate 57*0Sstevel@tonic-gate LOCK_LOGREGION(dblp); 58*0Sstevel@tonic-gate ret = __log_put(dblp, lsn, dbt, flags); 59*0Sstevel@tonic-gate UNLOCK_LOGREGION(dblp); 60*0Sstevel@tonic-gate return (ret); 61*0Sstevel@tonic-gate } 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate /* 64*0Sstevel@tonic-gate * __log_put -- 65*0Sstevel@tonic-gate * Write a log record; internal version. 66*0Sstevel@tonic-gate * 67*0Sstevel@tonic-gate * PUBLIC: int __log_put __P((DB_LOG *, DB_LSN *, const DBT *, u_int32_t)); 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate int 70*0Sstevel@tonic-gate __log_put(dblp, lsn, dbt, flags) 71*0Sstevel@tonic-gate DB_LOG *dblp; 72*0Sstevel@tonic-gate DB_LSN *lsn; 73*0Sstevel@tonic-gate const DBT *dbt; 74*0Sstevel@tonic-gate u_int32_t flags; 75*0Sstevel@tonic-gate { 76*0Sstevel@tonic-gate DBT fid_dbt, t; 77*0Sstevel@tonic-gate DB_LSN r_unused; 78*0Sstevel@tonic-gate FNAME *fnp; 79*0Sstevel@tonic-gate LOG *lp; 80*0Sstevel@tonic-gate u_int32_t lastoff; 81*0Sstevel@tonic-gate int ret; 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate lp = dblp->lp; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate /* 86*0Sstevel@tonic-gate * If the application just wants to know where we are, fill in 87*0Sstevel@tonic-gate * the information. Currently used by the transaction manager 88*0Sstevel@tonic-gate * to avoid writing TXN_begin records. 89*0Sstevel@tonic-gate */ 90*0Sstevel@tonic-gate if (flags == DB_CURLSN) { 91*0Sstevel@tonic-gate lsn->file = lp->lsn.file; 92*0Sstevel@tonic-gate lsn->offset = lp->lsn.offset; 93*0Sstevel@tonic-gate return (0); 94*0Sstevel@tonic-gate } 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate /* If this information won't fit in the file, swap files. */ 97*0Sstevel@tonic-gate if (lp->lsn.offset + sizeof(HDR) + dbt->size > lp->persist.lg_max) { 98*0Sstevel@tonic-gate if (sizeof(HDR) + 99*0Sstevel@tonic-gate sizeof(LOGP) + dbt->size > lp->persist.lg_max) { 100*0Sstevel@tonic-gate __db_err(dblp->dbenv, 101*0Sstevel@tonic-gate "log_put: record larger than maximum file size"); 102*0Sstevel@tonic-gate return (EINVAL); 103*0Sstevel@tonic-gate } 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate /* Flush the log. */ 106*0Sstevel@tonic-gate if ((ret = __log_flush(dblp, NULL)) != 0) 107*0Sstevel@tonic-gate return (ret); 108*0Sstevel@tonic-gate 109*0Sstevel@tonic-gate /* 110*0Sstevel@tonic-gate * Save the last known offset from the previous file, we'll 111*0Sstevel@tonic-gate * need it to initialize the persistent header information. 112*0Sstevel@tonic-gate */ 113*0Sstevel@tonic-gate lastoff = lp->lsn.offset; 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gate /* Point the current LSN to the new file. */ 116*0Sstevel@tonic-gate ++lp->lsn.file; 117*0Sstevel@tonic-gate lp->lsn.offset = 0; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate /* Reset the file write offset. */ 120*0Sstevel@tonic-gate lp->w_off = 0; 121*0Sstevel@tonic-gate } else 122*0Sstevel@tonic-gate lastoff = 0; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate /* Initialize the LSN information returned to the user. */ 125*0Sstevel@tonic-gate lsn->file = lp->lsn.file; 126*0Sstevel@tonic-gate lsn->offset = lp->lsn.offset; 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate /* 129*0Sstevel@tonic-gate * Insert persistent information as the first record in every file. 130*0Sstevel@tonic-gate * Note that the previous length is wrong for the very first record 131*0Sstevel@tonic-gate * of the log, but that's okay, we check for it during retrieval. 132*0Sstevel@tonic-gate */ 133*0Sstevel@tonic-gate if (lp->lsn.offset == 0) { 134*0Sstevel@tonic-gate t.data = &lp->persist; 135*0Sstevel@tonic-gate t.size = sizeof(LOGP); 136*0Sstevel@tonic-gate if ((ret = __log_putr(dblp, lsn, 137*0Sstevel@tonic-gate &t, lastoff == 0 ? 0 : lastoff - lp->len)) != 0) 138*0Sstevel@tonic-gate return (ret); 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate /* Update the LSN information returned to the user. */ 141*0Sstevel@tonic-gate lsn->file = lp->lsn.file; 142*0Sstevel@tonic-gate lsn->offset = lp->lsn.offset; 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* Write the application's log record. */ 146*0Sstevel@tonic-gate if ((ret = __log_putr(dblp, lsn, dbt, lp->lsn.offset - lp->len)) != 0) 147*0Sstevel@tonic-gate return (ret); 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate /* 150*0Sstevel@tonic-gate * On a checkpoint, we: 151*0Sstevel@tonic-gate * Put out the checkpoint record (above). 152*0Sstevel@tonic-gate * Save the LSN of the checkpoint in the shared region. 153*0Sstevel@tonic-gate * Append the set of file name information into the log. 154*0Sstevel@tonic-gate */ 155*0Sstevel@tonic-gate if (flags == DB_CHECKPOINT) { 156*0Sstevel@tonic-gate lp->chkpt_lsn = *lsn; 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate for (fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname); 159*0Sstevel@tonic-gate fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname)) { 160*0Sstevel@tonic-gate if (fnp->ref == 0) /* Entry not in use. */ 161*0Sstevel@tonic-gate continue; 162*0Sstevel@tonic-gate memset(&t, 0, sizeof(t)); 163*0Sstevel@tonic-gate t.data = R_ADDR(dblp, fnp->name_off); 164*0Sstevel@tonic-gate t.size = strlen(t.data) + 1; 165*0Sstevel@tonic-gate memset(&fid_dbt, 0, sizeof(fid_dbt)); 166*0Sstevel@tonic-gate fid_dbt.data = fnp->ufid; 167*0Sstevel@tonic-gate fid_dbt.size = DB_FILE_ID_LEN; 168*0Sstevel@tonic-gate if ((ret = __log_register_log(dblp, NULL, &r_unused, 0, 169*0Sstevel@tonic-gate LOG_CHECKPOINT, &t, &fid_dbt, fnp->id, fnp->s_type)) 170*0Sstevel@tonic-gate != 0) 171*0Sstevel@tonic-gate return (ret); 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate } 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate /* 176*0Sstevel@tonic-gate * On a checkpoint or when flush is requested, we: 177*0Sstevel@tonic-gate * Flush the current buffer contents to disk. 178*0Sstevel@tonic-gate * Sync the log to disk. 179*0Sstevel@tonic-gate */ 180*0Sstevel@tonic-gate if (flags == DB_FLUSH || flags == DB_CHECKPOINT) 181*0Sstevel@tonic-gate if ((ret = __log_flush(dblp, NULL)) != 0) 182*0Sstevel@tonic-gate return (ret); 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate /* 185*0Sstevel@tonic-gate * On a checkpoint, we: 186*0Sstevel@tonic-gate * Save the time the checkpoint was written. 187*0Sstevel@tonic-gate * Reset the bytes written since the last checkpoint. 188*0Sstevel@tonic-gate */ 189*0Sstevel@tonic-gate if (flags == DB_CHECKPOINT) { 190*0Sstevel@tonic-gate (void)time(&lp->chkpt); 191*0Sstevel@tonic-gate lp->stat.st_wc_bytes = lp->stat.st_wc_mbytes = 0; 192*0Sstevel@tonic-gate } 193*0Sstevel@tonic-gate return (0); 194*0Sstevel@tonic-gate } 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate /* 197*0Sstevel@tonic-gate * __log_putr -- 198*0Sstevel@tonic-gate * Actually put a record into the log. 199*0Sstevel@tonic-gate */ 200*0Sstevel@tonic-gate static int 201*0Sstevel@tonic-gate __log_putr(dblp, lsn, dbt, prev) 202*0Sstevel@tonic-gate DB_LOG *dblp; 203*0Sstevel@tonic-gate DB_LSN *lsn; 204*0Sstevel@tonic-gate const DBT *dbt; 205*0Sstevel@tonic-gate u_int32_t prev; 206*0Sstevel@tonic-gate { 207*0Sstevel@tonic-gate HDR hdr; 208*0Sstevel@tonic-gate LOG *lp; 209*0Sstevel@tonic-gate int ret; 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate lp = dblp->lp; 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate /* 214*0Sstevel@tonic-gate * Initialize the header. If we just switched files, lsn.offset will 215*0Sstevel@tonic-gate * be 0, and what we really want is the offset of the previous record 216*0Sstevel@tonic-gate * in the previous file. Fortunately, prev holds the value we want. 217*0Sstevel@tonic-gate */ 218*0Sstevel@tonic-gate hdr.prev = prev; 219*0Sstevel@tonic-gate hdr.len = sizeof(HDR) + dbt->size; 220*0Sstevel@tonic-gate hdr.cksum = __ham_func4(dbt->data, dbt->size); 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate if ((ret = __log_fill(dblp, lsn, &hdr, sizeof(HDR))) != 0) 223*0Sstevel@tonic-gate return (ret); 224*0Sstevel@tonic-gate lp->len = sizeof(HDR); 225*0Sstevel@tonic-gate lp->lsn.offset += sizeof(HDR); 226*0Sstevel@tonic-gate 227*0Sstevel@tonic-gate if ((ret = __log_fill(dblp, lsn, dbt->data, dbt->size)) != 0) 228*0Sstevel@tonic-gate return (ret); 229*0Sstevel@tonic-gate lp->len += dbt->size; 230*0Sstevel@tonic-gate lp->lsn.offset += dbt->size; 231*0Sstevel@tonic-gate return (0); 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate /* 235*0Sstevel@tonic-gate * log_flush -- 236*0Sstevel@tonic-gate * Write all records less than or equal to the specified LSN. 237*0Sstevel@tonic-gate */ 238*0Sstevel@tonic-gate int 239*0Sstevel@tonic-gate log_flush(dblp, lsn) 240*0Sstevel@tonic-gate DB_LOG *dblp; 241*0Sstevel@tonic-gate const DB_LSN *lsn; 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate int ret; 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate LOG_PANIC_CHECK(dblp); 246*0Sstevel@tonic-gate 247*0Sstevel@tonic-gate LOCK_LOGREGION(dblp); 248*0Sstevel@tonic-gate ret = __log_flush(dblp, lsn); 249*0Sstevel@tonic-gate UNLOCK_LOGREGION(dblp); 250*0Sstevel@tonic-gate return (ret); 251*0Sstevel@tonic-gate } 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate /* 254*0Sstevel@tonic-gate * __log_flush -- 255*0Sstevel@tonic-gate * Write all records less than or equal to the specified LSN; internal 256*0Sstevel@tonic-gate * version. 257*0Sstevel@tonic-gate */ 258*0Sstevel@tonic-gate static int 259*0Sstevel@tonic-gate __log_flush(dblp, lsn) 260*0Sstevel@tonic-gate DB_LOG *dblp; 261*0Sstevel@tonic-gate const DB_LSN *lsn; 262*0Sstevel@tonic-gate { 263*0Sstevel@tonic-gate DB_LSN t_lsn; 264*0Sstevel@tonic-gate LOG *lp; 265*0Sstevel@tonic-gate int current, ret; 266*0Sstevel@tonic-gate 267*0Sstevel@tonic-gate ret = 0; 268*0Sstevel@tonic-gate lp = dblp->lp; 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gate /* 271*0Sstevel@tonic-gate * If no LSN specified, flush the entire log by setting the flush LSN 272*0Sstevel@tonic-gate * to the last LSN written in the log. Otherwise, check that the LSN 273*0Sstevel@tonic-gate * isn't a non-existent record for the log. 274*0Sstevel@tonic-gate */ 275*0Sstevel@tonic-gate if (lsn == NULL) { 276*0Sstevel@tonic-gate t_lsn.file = lp->lsn.file; 277*0Sstevel@tonic-gate t_lsn.offset = lp->lsn.offset - lp->len; 278*0Sstevel@tonic-gate lsn = &t_lsn; 279*0Sstevel@tonic-gate } else 280*0Sstevel@tonic-gate if (lsn->file > lp->lsn.file || 281*0Sstevel@tonic-gate (lsn->file == lp->lsn.file && 282*0Sstevel@tonic-gate lsn->offset > lp->lsn.offset - lp->len)) { 283*0Sstevel@tonic-gate __db_err(dblp->dbenv, 284*0Sstevel@tonic-gate "log_flush: LSN past current end-of-log"); 285*0Sstevel@tonic-gate return (EINVAL); 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate /* 289*0Sstevel@tonic-gate * If the LSN is less than the last-sync'd LSN, we're done. Note, 290*0Sstevel@tonic-gate * the last-sync LSN saved in s_lsn is the LSN of the first byte 291*0Sstevel@tonic-gate * we absolutely know has been written to disk, so the test is <=. 292*0Sstevel@tonic-gate */ 293*0Sstevel@tonic-gate if (lsn->file < lp->s_lsn.file || 294*0Sstevel@tonic-gate (lsn->file == lp->s_lsn.file && lsn->offset <= lp->s_lsn.offset)) 295*0Sstevel@tonic-gate return (0); 296*0Sstevel@tonic-gate 297*0Sstevel@tonic-gate /* 298*0Sstevel@tonic-gate * We may need to write the current buffer. We have to write the 299*0Sstevel@tonic-gate * current buffer if the flush LSN is greater than or equal to the 300*0Sstevel@tonic-gate * buffer's starting LSN. 301*0Sstevel@tonic-gate */ 302*0Sstevel@tonic-gate current = 0; 303*0Sstevel@tonic-gate if (lp->b_off != 0 && log_compare(lsn, &lp->f_lsn) >= 0) { 304*0Sstevel@tonic-gate if ((ret = __log_write(dblp, lp->buf, lp->b_off)) != 0) 305*0Sstevel@tonic-gate return (ret); 306*0Sstevel@tonic-gate 307*0Sstevel@tonic-gate lp->b_off = 0; 308*0Sstevel@tonic-gate current = 1; 309*0Sstevel@tonic-gate } 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate /* 312*0Sstevel@tonic-gate * It's possible that this thread may never have written to this log 313*0Sstevel@tonic-gate * file. Acquire a file descriptor if we don't already have one. 314*0Sstevel@tonic-gate */ 315*0Sstevel@tonic-gate if (dblp->lfname != dblp->lp->lsn.file) 316*0Sstevel@tonic-gate if ((ret = __log_newfd(dblp)) != 0) 317*0Sstevel@tonic-gate return (ret); 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate /* Sync all writes to disk. */ 320*0Sstevel@tonic-gate if ((ret = __os_fsync(dblp->lfd)) != 0) { 321*0Sstevel@tonic-gate __db_panic(dblp->dbenv, ret); 322*0Sstevel@tonic-gate return (ret); 323*0Sstevel@tonic-gate } 324*0Sstevel@tonic-gate ++lp->stat.st_scount; 325*0Sstevel@tonic-gate 326*0Sstevel@tonic-gate /* 327*0Sstevel@tonic-gate * Set the last-synced LSN, using the LSN of the current buffer. If 328*0Sstevel@tonic-gate * the current buffer was flushed, we know the LSN of the first byte 329*0Sstevel@tonic-gate * of the buffer is on disk, otherwise, we only know that the LSN of 330*0Sstevel@tonic-gate * the record before the one beginning the current buffer is on disk. 331*0Sstevel@tonic-gate * 332*0Sstevel@tonic-gate * XXX 333*0Sstevel@tonic-gate * Check to make sure that the saved lsn isn't 0 before we go making 334*0Sstevel@tonic-gate * this change. If DB_CHECKPOINT was called before we actually wrote 335*0Sstevel@tonic-gate * something, you can end up here without ever having written anything 336*0Sstevel@tonic-gate * to a log file, and decrementing either s_lsn.file or s_lsn.offset 337*0Sstevel@tonic-gate * will cause much sadness later on. 338*0Sstevel@tonic-gate */ 339*0Sstevel@tonic-gate lp->s_lsn = lp->f_lsn; 340*0Sstevel@tonic-gate if (!current && lp->s_lsn.file != 0) 341*0Sstevel@tonic-gate if (lp->s_lsn.offset == 0) { 342*0Sstevel@tonic-gate --lp->s_lsn.file; 343*0Sstevel@tonic-gate lp->s_lsn.offset = lp->persist.lg_max; 344*0Sstevel@tonic-gate } else 345*0Sstevel@tonic-gate --lp->s_lsn.offset; 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate return (0); 348*0Sstevel@tonic-gate } 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate /* 351*0Sstevel@tonic-gate * __log_fill -- 352*0Sstevel@tonic-gate * Write information into the log. 353*0Sstevel@tonic-gate */ 354*0Sstevel@tonic-gate static int 355*0Sstevel@tonic-gate __log_fill(dblp, lsn, addr, len) 356*0Sstevel@tonic-gate DB_LOG *dblp; 357*0Sstevel@tonic-gate DB_LSN *lsn; 358*0Sstevel@tonic-gate void *addr; 359*0Sstevel@tonic-gate u_int32_t len; 360*0Sstevel@tonic-gate { 361*0Sstevel@tonic-gate LOG *lp; 362*0Sstevel@tonic-gate u_int32_t nrec; 363*0Sstevel@tonic-gate size_t nw, remain; 364*0Sstevel@tonic-gate int ret; 365*0Sstevel@tonic-gate 366*0Sstevel@tonic-gate /* Copy out the data. */ 367*0Sstevel@tonic-gate for (lp = dblp->lp; len > 0;) { 368*0Sstevel@tonic-gate /* 369*0Sstevel@tonic-gate * If we're beginning a new buffer, note the user LSN to which 370*0Sstevel@tonic-gate * the first byte of the buffer belongs. We have to know this 371*0Sstevel@tonic-gate * when flushing the buffer so that we know if the in-memory 372*0Sstevel@tonic-gate * buffer needs to be flushed. 373*0Sstevel@tonic-gate */ 374*0Sstevel@tonic-gate if (lp->b_off == 0) 375*0Sstevel@tonic-gate lp->f_lsn = *lsn; 376*0Sstevel@tonic-gate 377*0Sstevel@tonic-gate /* 378*0Sstevel@tonic-gate * If we're on a buffer boundary and the data is big enough, 379*0Sstevel@tonic-gate * copy as many records as we can directly from the data. 380*0Sstevel@tonic-gate */ 381*0Sstevel@tonic-gate if (lp->b_off == 0 && len >= sizeof(lp->buf)) { 382*0Sstevel@tonic-gate nrec = len / sizeof(lp->buf); 383*0Sstevel@tonic-gate if ((ret = __log_write(dblp, 384*0Sstevel@tonic-gate addr, nrec * sizeof(lp->buf))) != 0) 385*0Sstevel@tonic-gate return (ret); 386*0Sstevel@tonic-gate addr = (u_int8_t *)addr + nrec * sizeof(lp->buf); 387*0Sstevel@tonic-gate len -= nrec * sizeof(lp->buf); 388*0Sstevel@tonic-gate continue; 389*0Sstevel@tonic-gate } 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gate /* Figure out how many bytes we can copy this time. */ 392*0Sstevel@tonic-gate remain = sizeof(lp->buf) - lp->b_off; 393*0Sstevel@tonic-gate nw = remain > len ? len : remain; 394*0Sstevel@tonic-gate memcpy(lp->buf + lp->b_off, addr, nw); 395*0Sstevel@tonic-gate addr = (u_int8_t *)addr + nw; 396*0Sstevel@tonic-gate len -= nw; 397*0Sstevel@tonic-gate lp->b_off += nw; 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate /* If we fill the buffer, flush it. */ 400*0Sstevel@tonic-gate if (lp->b_off == sizeof(lp->buf)) { 401*0Sstevel@tonic-gate if ((ret = 402*0Sstevel@tonic-gate __log_write(dblp, lp->buf, sizeof(lp->buf))) != 0) 403*0Sstevel@tonic-gate return (ret); 404*0Sstevel@tonic-gate lp->b_off = 0; 405*0Sstevel@tonic-gate } 406*0Sstevel@tonic-gate } 407*0Sstevel@tonic-gate return (0); 408*0Sstevel@tonic-gate } 409*0Sstevel@tonic-gate 410*0Sstevel@tonic-gate /* 411*0Sstevel@tonic-gate * __log_write -- 412*0Sstevel@tonic-gate * Write the log buffer to disk. 413*0Sstevel@tonic-gate */ 414*0Sstevel@tonic-gate static int 415*0Sstevel@tonic-gate __log_write(dblp, addr, len) 416*0Sstevel@tonic-gate DB_LOG *dblp; 417*0Sstevel@tonic-gate void *addr; 418*0Sstevel@tonic-gate u_int32_t len; 419*0Sstevel@tonic-gate { 420*0Sstevel@tonic-gate LOG *lp; 421*0Sstevel@tonic-gate ssize_t nw; 422*0Sstevel@tonic-gate int ret; 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate /* 425*0Sstevel@tonic-gate * If we haven't opened the log file yet or the current one 426*0Sstevel@tonic-gate * has changed, acquire a new log file. 427*0Sstevel@tonic-gate */ 428*0Sstevel@tonic-gate lp = dblp->lp; 429*0Sstevel@tonic-gate if (dblp->lfd == -1 || dblp->lfname != lp->lsn.file) 430*0Sstevel@tonic-gate if ((ret = __log_newfd(dblp)) != 0) 431*0Sstevel@tonic-gate return (ret); 432*0Sstevel@tonic-gate 433*0Sstevel@tonic-gate /* 434*0Sstevel@tonic-gate * Seek to the offset in the file (someone may have written it 435*0Sstevel@tonic-gate * since we last did). 436*0Sstevel@tonic-gate */ 437*0Sstevel@tonic-gate if ((ret = __os_seek(dblp->lfd, 0, 0, lp->w_off, 0, SEEK_SET)) != 0 || 438*0Sstevel@tonic-gate (ret = __os_write(dblp->lfd, addr, len, &nw)) != 0) { 439*0Sstevel@tonic-gate __db_panic(dblp->dbenv, ret); 440*0Sstevel@tonic-gate return (ret); 441*0Sstevel@tonic-gate } 442*0Sstevel@tonic-gate if (nw != (int32_t)len) 443*0Sstevel@tonic-gate return (EIO); 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gate /* Reset the buffer offset and update the seek offset. */ 446*0Sstevel@tonic-gate lp->w_off += len; 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate /* Update written statistics. */ 449*0Sstevel@tonic-gate if ((lp->stat.st_w_bytes += len) >= MEGABYTE) { 450*0Sstevel@tonic-gate lp->stat.st_w_bytes -= MEGABYTE; 451*0Sstevel@tonic-gate ++lp->stat.st_w_mbytes; 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate if ((lp->stat.st_wc_bytes += len) >= MEGABYTE) { 454*0Sstevel@tonic-gate lp->stat.st_wc_bytes -= MEGABYTE; 455*0Sstevel@tonic-gate ++lp->stat.st_wc_mbytes; 456*0Sstevel@tonic-gate } 457*0Sstevel@tonic-gate ++lp->stat.st_wcount; 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate return (0); 460*0Sstevel@tonic-gate } 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gate /* 463*0Sstevel@tonic-gate * log_file -- 464*0Sstevel@tonic-gate * Map a DB_LSN to a file name. 465*0Sstevel@tonic-gate */ 466*0Sstevel@tonic-gate int 467*0Sstevel@tonic-gate log_file(dblp, lsn, namep, len) 468*0Sstevel@tonic-gate DB_LOG *dblp; 469*0Sstevel@tonic-gate const DB_LSN *lsn; 470*0Sstevel@tonic-gate char *namep; 471*0Sstevel@tonic-gate size_t len; 472*0Sstevel@tonic-gate { 473*0Sstevel@tonic-gate int ret; 474*0Sstevel@tonic-gate char *name; 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate LOG_PANIC_CHECK(dblp); 477*0Sstevel@tonic-gate 478*0Sstevel@tonic-gate LOCK_LOGREGION(dblp); 479*0Sstevel@tonic-gate ret = __log_name(dblp, lsn->file, &name, NULL, 0); 480*0Sstevel@tonic-gate UNLOCK_LOGREGION(dblp); 481*0Sstevel@tonic-gate if (ret != 0) 482*0Sstevel@tonic-gate return (ret); 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gate /* Check to make sure there's enough room and copy the name. */ 485*0Sstevel@tonic-gate if (len < strlen(name) + 1) { 486*0Sstevel@tonic-gate *namep = '\0'; 487*0Sstevel@tonic-gate return (ENOMEM); 488*0Sstevel@tonic-gate } 489*0Sstevel@tonic-gate (void)strcpy(namep, name); 490*0Sstevel@tonic-gate __os_freestr(name); 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate return (0); 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate /* 496*0Sstevel@tonic-gate * __log_newfd -- 497*0Sstevel@tonic-gate * Acquire a file descriptor for the current log file. 498*0Sstevel@tonic-gate */ 499*0Sstevel@tonic-gate static int 500*0Sstevel@tonic-gate __log_newfd(dblp) 501*0Sstevel@tonic-gate DB_LOG *dblp; 502*0Sstevel@tonic-gate { 503*0Sstevel@tonic-gate int ret; 504*0Sstevel@tonic-gate char *name; 505*0Sstevel@tonic-gate 506*0Sstevel@tonic-gate /* Close any previous file descriptor. */ 507*0Sstevel@tonic-gate if (dblp->lfd != -1) { 508*0Sstevel@tonic-gate (void)__os_close(dblp->lfd); 509*0Sstevel@tonic-gate dblp->lfd = -1; 510*0Sstevel@tonic-gate } 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gate /* Get the path of the new file and open it. */ 513*0Sstevel@tonic-gate dblp->lfname = dblp->lp->lsn.file; 514*0Sstevel@tonic-gate if ((ret = __log_name(dblp, 515*0Sstevel@tonic-gate dblp->lfname, &name, &dblp->lfd, DB_CREATE | DB_SEQUENTIAL)) != 0) 516*0Sstevel@tonic-gate __db_err(dblp->dbenv, "log_put: %s: %s", name, strerror(ret)); 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate __os_freestr(name); 519*0Sstevel@tonic-gate return (ret); 520*0Sstevel@tonic-gate } 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate /* 523*0Sstevel@tonic-gate * __log_name -- 524*0Sstevel@tonic-gate * Return the log name for a particular file, and optionally open it. 525*0Sstevel@tonic-gate * 526*0Sstevel@tonic-gate * PUBLIC: int __log_name __P((DB_LOG *, u_int32_t, char **, int *, u_int32_t)); 527*0Sstevel@tonic-gate */ 528*0Sstevel@tonic-gate int 529*0Sstevel@tonic-gate __log_name(dblp, filenumber, namep, fdp, flags) 530*0Sstevel@tonic-gate DB_LOG *dblp; 531*0Sstevel@tonic-gate u_int32_t filenumber, flags; 532*0Sstevel@tonic-gate char **namep; 533*0Sstevel@tonic-gate int *fdp; 534*0Sstevel@tonic-gate { 535*0Sstevel@tonic-gate int ret; 536*0Sstevel@tonic-gate char *oname; 537*0Sstevel@tonic-gate char old[sizeof(LFPREFIX) + 5 + 20], new[sizeof(LFPREFIX) + 10 + 20]; 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate /* 540*0Sstevel@tonic-gate * !!! 541*0Sstevel@tonic-gate * The semantics of this routine are bizarre. 542*0Sstevel@tonic-gate * 543*0Sstevel@tonic-gate * The reason for all of this is that we need a place where we can 544*0Sstevel@tonic-gate * intercept requests for log files, and, if appropriate, check for 545*0Sstevel@tonic-gate * both the old-style and new-style log file names. The trick is 546*0Sstevel@tonic-gate * that all callers of this routine that are opening the log file 547*0Sstevel@tonic-gate * read-only want to use an old-style file name if they can't find 548*0Sstevel@tonic-gate * a match using a new-style name. The only down-side is that some 549*0Sstevel@tonic-gate * callers may check for the old-style when they really don't need 550*0Sstevel@tonic-gate * to, but that shouldn't mess up anything, and we only check for 551*0Sstevel@tonic-gate * the old-style name when we've already failed to find a new-style 552*0Sstevel@tonic-gate * one. 553*0Sstevel@tonic-gate * 554*0Sstevel@tonic-gate * Create a new-style file name, and if we're not going to open the 555*0Sstevel@tonic-gate * file, return regardless. 556*0Sstevel@tonic-gate */ 557*0Sstevel@tonic-gate (void)snprintf(new, sizeof(new), LFNAME, filenumber); 558*0Sstevel@tonic-gate if ((ret = __db_appname(dblp->dbenv, 559*0Sstevel@tonic-gate DB_APP_LOG, dblp->dir, new, 0, NULL, namep)) != 0 || fdp == NULL) 560*0Sstevel@tonic-gate return (ret); 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate /* Open the new-style file -- if we succeed, we're done. */ 563*0Sstevel@tonic-gate if ((ret = __db_open(*namep, 564*0Sstevel@tonic-gate flags, flags, dblp->lp->persist.mode, fdp)) == 0) 565*0Sstevel@tonic-gate return (0); 566*0Sstevel@tonic-gate 567*0Sstevel@tonic-gate /* 568*0Sstevel@tonic-gate * The open failed... if the DB_RDONLY flag isn't set, we're done, 569*0Sstevel@tonic-gate * the caller isn't interested in old-style files. 570*0Sstevel@tonic-gate */ 571*0Sstevel@tonic-gate if (!LF_ISSET(DB_RDONLY)) 572*0Sstevel@tonic-gate return (ret); 573*0Sstevel@tonic-gate 574*0Sstevel@tonic-gate /* Create an old-style file name. */ 575*0Sstevel@tonic-gate (void)snprintf(old, sizeof(old), LFNAME_V1, filenumber); 576*0Sstevel@tonic-gate if ((ret = __db_appname(dblp->dbenv, 577*0Sstevel@tonic-gate DB_APP_LOG, dblp->dir, old, 0, NULL, &oname)) != 0) 578*0Sstevel@tonic-gate goto err; 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate /* 581*0Sstevel@tonic-gate * Open the old-style file -- if we succeed, we're done. Free the 582*0Sstevel@tonic-gate * space allocated for the new-style name and return the old-style 583*0Sstevel@tonic-gate * name to the caller. 584*0Sstevel@tonic-gate */ 585*0Sstevel@tonic-gate if ((ret = __db_open(oname, 586*0Sstevel@tonic-gate flags, flags, dblp->lp->persist.mode, fdp)) == 0) { 587*0Sstevel@tonic-gate __os_freestr(*namep); 588*0Sstevel@tonic-gate *namep = oname; 589*0Sstevel@tonic-gate return (0); 590*0Sstevel@tonic-gate } 591*0Sstevel@tonic-gate 592*0Sstevel@tonic-gate /* 593*0Sstevel@tonic-gate * Couldn't find either style of name -- return the new-style name 594*0Sstevel@tonic-gate * for the caller's error message. If it's an old-style name that's 595*0Sstevel@tonic-gate * actually missing we're going to confuse the user with the error 596*0Sstevel@tonic-gate * message, but that implies that not only were we looking for an 597*0Sstevel@tonic-gate * old-style name, but we expected it to exist and we weren't just 598*0Sstevel@tonic-gate * looking for any log file. That's not a likely error. 599*0Sstevel@tonic-gate */ 600*0Sstevel@tonic-gate err: __os_freestr(oname); 601*0Sstevel@tonic-gate return (ret); 602*0Sstevel@tonic-gate } 603