#include "config.h"
#ifndef lint
static const char sccsid[] = "@(#)log_put.c 10.44 (Sleepycat) 11/3/98";
#endif
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#endif
#include "db_int.h"
#include "shqueue.h"
#include "db_page.h"
#include "log.h"
#include "hash.h"
#include "clib_ext.h"
#include "common_ext.h"
static int __log_fill __P((DB_LOG *, DB_LSN *, void *, u_int32_t));
static int __log_flush __P((DB_LOG *, const DB_LSN *));
static int __log_newfd __P((DB_LOG *));
static int __log_putr __P((DB_LOG *, DB_LSN *, const DBT *, u_int32_t));
static int __log_write __P((DB_LOG *, void *, u_int32_t));
int
log_put(dblp, lsn, dbt, flags)
DB_LOG *dblp;
DB_LSN *lsn;
const DBT *dbt;
u_int32_t flags;
{
int ret;
LOG_PANIC_CHECK(dblp);
if (flags != 0 && flags != DB_CHECKPOINT &&
flags != DB_CURLSN && flags != DB_FLUSH)
return (__db_ferr(dblp->dbenv, "log_put", 0));
LOCK_LOGREGION(dblp);
ret = __log_put(dblp, lsn, dbt, flags);
UNLOCK_LOGREGION(dblp);
return (ret);
}
int
__log_put(dblp, lsn, dbt, flags)
DB_LOG *dblp;
DB_LSN *lsn;
const DBT *dbt;
u_int32_t flags;
{
DBT fid_dbt, t;
DB_LSN r_unused;
FNAME *fnp;
LOG *lp;
u_int32_t lastoff;
int ret;
lp = dblp->lp;
if (flags == DB_CURLSN) {
lsn->file = lp->lsn.file;
lsn->offset = lp->lsn.offset;
return (0);
}
if (lp->lsn.offset + sizeof(HDR) + dbt->size > lp->persist.lg_max) {
if (sizeof(HDR) +
sizeof(LOGP) + dbt->size > lp->persist.lg_max) {
__db_err(dblp->dbenv,
"log_put: record larger than maximum file size");
return (EINVAL);
}
if ((ret = __log_flush(dblp, NULL)) != 0)
return (ret);
lastoff = lp->lsn.offset;
++lp->lsn.file;
lp->lsn.offset = 0;
lp->w_off = 0;
} else
lastoff = 0;
lsn->file = lp->lsn.file;
lsn->offset = lp->lsn.offset;
if (lp->lsn.offset == 0) {
t.data = &lp->persist;
t.size = sizeof(LOGP);
if ((ret = __log_putr(dblp, lsn,
&t, lastoff == 0 ? 0 : lastoff - lp->len)) != 0)
return (ret);
lsn->file = lp->lsn.file;
lsn->offset = lp->lsn.offset;
}
if ((ret = __log_putr(dblp, lsn, dbt, lp->lsn.offset - lp->len)) != 0)
return (ret);
if (flags == DB_CHECKPOINT) {
lp->chkpt_lsn = *lsn;
for (fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname);
fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname)) {
if (fnp->ref == 0)
continue;
memset(&t, 0, sizeof(t));
t.data = R_ADDR(dblp, fnp->name_off);
t.size = strlen(t.data) + 1;
memset(&fid_dbt, 0, sizeof(fid_dbt));
fid_dbt.data = fnp->ufid;
fid_dbt.size = DB_FILE_ID_LEN;
if ((ret = __log_register_log(dblp, NULL, &r_unused, 0,
LOG_CHECKPOINT, &t, &fid_dbt, fnp->id, fnp->s_type))
!= 0)
return (ret);
}
}
if (flags == DB_FLUSH || flags == DB_CHECKPOINT)
if ((ret = __log_flush(dblp, NULL)) != 0)
return (ret);
if (flags == DB_CHECKPOINT) {
(void)time(&lp->chkpt);
lp->stat.st_wc_bytes = lp->stat.st_wc_mbytes = 0;
}
return (0);
}
static int
__log_putr(dblp, lsn, dbt, prev)
DB_LOG *dblp;
DB_LSN *lsn;
const DBT *dbt;
u_int32_t prev;
{
HDR hdr;
LOG *lp;
int ret;
lp = dblp->lp;
hdr.prev = prev;
hdr.len = sizeof(HDR) + dbt->size;
hdr.cksum = __ham_func4(dbt->data, dbt->size);
if ((ret = __log_fill(dblp, lsn, &hdr, sizeof(HDR))) != 0)
return (ret);
lp->len = sizeof(HDR);
lp->lsn.offset += sizeof(HDR);
if ((ret = __log_fill(dblp, lsn, dbt->data, dbt->size)) != 0)
return (ret);
lp->len += dbt->size;
lp->lsn.offset += dbt->size;
return (0);
}
int
log_flush(dblp, lsn)
DB_LOG *dblp;
const DB_LSN *lsn;
{
int ret;
LOG_PANIC_CHECK(dblp);
LOCK_LOGREGION(dblp);
ret = __log_flush(dblp, lsn);
UNLOCK_LOGREGION(dblp);
return (ret);
}
static int
__log_flush(dblp, lsn)
DB_LOG *dblp;
const DB_LSN *lsn;
{
DB_LSN t_lsn;
LOG *lp;
int current, ret;
ret = 0;
lp = dblp->lp;
if (lsn == NULL) {
t_lsn.file = lp->lsn.file;
t_lsn.offset = lp->lsn.offset - lp->len;
lsn = &t_lsn;
} else
if (lsn->file > lp->lsn.file ||
(lsn->file == lp->lsn.file &&
lsn->offset > lp->lsn.offset - lp->len)) {
__db_err(dblp->dbenv,
"log_flush: LSN past current end-of-log");
return (EINVAL);
}
if (lsn->file < lp->s_lsn.file ||
(lsn->file == lp->s_lsn.file && lsn->offset <= lp->s_lsn.offset))
return (0);
current = 0;
if (lp->b_off != 0 && log_compare(lsn, &lp->f_lsn) >= 0) {
if ((ret = __log_write(dblp, lp->buf, lp->b_off)) != 0)
return (ret);
lp->b_off = 0;
current = 1;
}
if (dblp->lfname != dblp->lp->lsn.file)
if ((ret = __log_newfd(dblp)) != 0)
return (ret);
if ((ret = __os_fsync(dblp->lfd)) != 0) {
__db_panic(dblp->dbenv, ret);
return (ret);
}
++lp->stat.st_scount;
lp->s_lsn = lp->f_lsn;
if (!current && lp->s_lsn.file != 0)
if (lp->s_lsn.offset == 0) {
--lp->s_lsn.file;
lp->s_lsn.offset = lp->persist.lg_max;
} else
--lp->s_lsn.offset;
return (0);
}
static int
__log_fill(dblp, lsn, addr, len)
DB_LOG *dblp;
DB_LSN *lsn;
void *addr;
u_int32_t len;
{
LOG *lp;
u_int32_t nrec;
size_t nw, remain;
int ret;
for (lp = dblp->lp; len > 0;) {
if (lp->b_off == 0)
lp->f_lsn = *lsn;
if (lp->b_off == 0 && len >= sizeof(lp->buf)) {
nrec = len / sizeof(lp->buf);
if ((ret = __log_write(dblp,
addr, nrec * sizeof(lp->buf))) != 0)
return (ret);
addr = (u_int8_t *)addr + nrec * sizeof(lp->buf);
len -= nrec * sizeof(lp->buf);
continue;
}
remain = sizeof(lp->buf) - lp->b_off;
nw = remain > len ? len : remain;
memcpy(lp->buf + lp->b_off, addr, nw);
addr = (u_int8_t *)addr + nw;
len -= nw;
lp->b_off += nw;
if (lp->b_off == sizeof(lp->buf)) {
if ((ret =
__log_write(dblp, lp->buf, sizeof(lp->buf))) != 0)
return (ret);
lp->b_off = 0;
}
}
return (0);
}
static int
__log_write(dblp, addr, len)
DB_LOG *dblp;
void *addr;
u_int32_t len;
{
LOG *lp;
ssize_t nw;
int ret;
lp = dblp->lp;
if (dblp->lfd == -1 || dblp->lfname != lp->lsn.file)
if ((ret = __log_newfd(dblp)) != 0)
return (ret);
if ((ret = __os_seek(dblp->lfd, 0, 0, lp->w_off, 0, SEEK_SET)) != 0 ||
(ret = __os_write(dblp->lfd, addr, len, &nw)) != 0) {
__db_panic(dblp->dbenv, ret);
return (ret);
}
if (nw != (int32_t)len)
return (EIO);
lp->w_off += len;
if ((lp->stat.st_w_bytes += len) >= MEGABYTE) {
lp->stat.st_w_bytes -= MEGABYTE;
++lp->stat.st_w_mbytes;
}
if ((lp->stat.st_wc_bytes += len) >= MEGABYTE) {
lp->stat.st_wc_bytes -= MEGABYTE;
++lp->stat.st_wc_mbytes;
}
++lp->stat.st_wcount;
return (0);
}
int
log_file(dblp, lsn, namep, len)
DB_LOG *dblp;
const DB_LSN *lsn;
char *namep;
size_t len;
{
int ret;
char *name;
LOG_PANIC_CHECK(dblp);
LOCK_LOGREGION(dblp);
ret = __log_name(dblp, lsn->file, &name, NULL, 0);
UNLOCK_LOGREGION(dblp);
if (ret != 0)
return (ret);
if (len < strlen(name) + 1) {
*namep = '\0';
return (ENOMEM);
}
(void)strcpy(namep, name);
__os_freestr(name);
return (0);
}
static int
__log_newfd(dblp)
DB_LOG *dblp;
{
int ret;
char *name;
if (dblp->lfd != -1) {
(void)__os_close(dblp->lfd);
dblp->lfd = -1;
}
dblp->lfname = dblp->lp->lsn.file;
if ((ret = __log_name(dblp,
dblp->lfname, &name, &dblp->lfd, DB_CREATE | DB_SEQUENTIAL)) != 0)
__db_err(dblp->dbenv, "log_put: %s: %s", name, strerror(ret));
__os_freestr(name);
return (ret);
}
int
__log_name(dblp, filenumber, namep, fdp, flags)
DB_LOG *dblp;
u_int32_t filenumber, flags;
char **namep;
int *fdp;
{
int ret;
char *oname;
char old[sizeof(LFPREFIX) + 5 + 20], new[sizeof(LFPREFIX) + 10 + 20];
(void)snprintf(new, sizeof(new), LFNAME, filenumber);
if ((ret = __db_appname(dblp->dbenv,
DB_APP_LOG, dblp->dir, new, 0, NULL, namep)) != 0 || fdp == NULL)
return (ret);
if ((ret = __db_open(*namep,
flags, flags, dblp->lp->persist.mode, fdp)) == 0)
return (0);
if (!LF_ISSET(DB_RDONLY))
return (ret);
(void)snprintf(old, sizeof(old), LFNAME_V1, filenumber);
if ((ret = __db_appname(dblp->dbenv,
DB_APP_LOG, dblp->dir, old, 0, NULL, &oname)) != 0)
goto err;
if ((ret = __db_open(oname,
flags, flags, dblp->lp->persist.mode, fdp)) == 0) {
__os_freestr(*namep);
*namep = oname;
return (0);
}
err: __os_freestr(oname);
return (ret);
}