2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
10 static const char sccsid[] = "@(#)log_put.c 10.14 (Sleepycat) 9/23/97";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
29 #include "common_ext.h"
31 static int __log_fill __P((DB_LOG *, void *, u_int32_t));
32 static int __log_newfd __P((DB_LOG *));
33 static int __log_write __P((DB_LOG *, void *, u_int32_t));
34 static int __log_putr __P((DB_LOG *, const DBT *, u_int32_t));
41 log_put(dblp, lsn, dbt, flags)
49 /* Validate arguments. */
50 #define OKFLAGS (DB_CHECKPOINT | DB_FLUSH)
53 __db_fchk(dblp->dbenv, "log_put", flags, OKFLAGS)) != 0)
61 return (__db_ferr(dblp->dbenv, "log_put", 1));
67 ret = __log_put(dblp, lsn, dbt, flags);
69 UNLOCK_LOGREGION(dblp);
76 * Write a log record; internal version.
78 * PUBLIC: int __log_put __P((DB_LOG *, DB_LSN *, const DBT *, int));
81 __log_put(dblp, lsn, dbt, flags)
97 /* If this information won't fit in the file, swap files. */
98 if (lp->lsn.offset + sizeof(HDR) + dbt->size > lp->persist.lg_max) {
100 sizeof(LOGP) + dbt->size > lp->persist.lg_max) {
101 __db_err(dblp->dbenv,
102 "log_put: record larger than maximum file size");
105 if (lp->b_off != 0) {
106 if ((ret = __log_write(dblp, lp->buf, lp->b_off)) != 0)
108 if ((ret = __db_fsync(dblp->lfd)) != 0)
110 lp->s_lsn.file = lp->lsn.file;
111 lp->s_lsn.offset = lp->lsn.offset - 1;
115 * Save the last known offset from the previous file, we'll
116 * need it to initialize the persistent header information.
118 lastoff = lp->lsn.offset;
127 * Insert persistent information as the first record in every file.
128 * Note that the previous length is wrong for the very first record
129 * of the log, but that's okay, we check for it during retrieval.
131 if (lp->lsn.offset == 0) {
132 t.data = &lp->persist;
133 t.size = sizeof(LOGP);
134 if ((ret = __log_putr(dblp,
135 &t, lastoff == 0 ? 0 : lastoff - lp->len)) != 0)
139 /* Initialize the LSN information returned to the user. */
140 lsn->file = lp->lsn.file;
141 lsn->offset = lp->lsn.offset;
143 /* Put out the user's record. */
144 if ((ret = __log_putr(dblp, dbt, lp->lsn.offset - lp->len)) != 0)
148 * On a checkpoint, we:
149 * Put out the checkpoint record (above).
150 * Save the LSN of the checkpoint in the shared region.
151 * Append the set of file name information into the log.
152 * Flush the current buffer contents to disk.
153 * Sync the log to disk.
154 * Save the time the checkpoint was written.
155 * Reset the bytes written since the last checkpoint.
157 if (flags == DB_CHECKPOINT) {
160 for (fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname);
161 fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname)) {
162 t.data = ADDR(dblp, fnp->name_off);
163 t.size = strlen(t.data) + 1;
164 memset(&fid_dbt, 0, sizeof(fid_dbt));
165 fid_dbt.data = ADDR(dblp, fnp->fileid_off);
166 fid_dbt.size = DB_FILE_ID_LEN;
167 if ((ret = __log_register_log(dblp, NULL, &r_unused,
168 0, &t, &fid_dbt, fnp->id, fnp->s_type)) != 0)
171 if (lp->b_off != 0 &&
172 (ret = __log_write(dblp, lp->buf, lp->b_off)) != 0)
174 (void)time(&lp->chkpt);
177 if ((ret = __db_fsync(dblp->lfd)) != 0)
179 lp->s_lsn.file = lp->lsn.file;
180 lp->s_lsn.offset = lp->lsn.offset - 1;
183 /* We always flush on a checkpoint. */
184 if (flags == DB_FLUSH || flags == DB_CHECKPOINT) {
185 if (lp->b_off != 0 &&
186 (ret = __log_write(dblp, lp->buf, lp->b_off)) != 0)
189 if ((ret = __db_fsync(dblp->lfd)) != 0)
191 lp->s_lsn.file = lp->lsn.file;
192 lp->s_lsn.offset = lp->lsn.offset - 1;
196 * If we just did I/O, i.e., this LSN could have spanned the start of
197 * the in-core buffer, we remember it so that we can flush correctly
200 if (lsn->offset < lp->w_off && lsn->offset + lp->len > lp->w_off)
207 * Actually put a record into the log.
210 __log_putr(dblp, dbt, prev)
222 * Initialize the header. If we just switched files, lsn.offset will
223 * be 0, and what we really want is the offset of the previous record
224 * in the previous file. Fortunately, prev holds the value we want.
227 hdr.len = sizeof(HDR) + dbt->size;
228 hdr.cksum = __ham_func4(dbt->data, dbt->size);
230 if ((ret = __log_fill(dblp, &hdr, sizeof(HDR))) != 0)
232 lp->lsn.offset += sizeof(HDR);
234 if ((ret = __log_fill(dblp, dbt->data, dbt->size)) != 0)
236 lp->lsn.offset += dbt->size;
238 lp->len = sizeof(HDR) + dbt->size;
244 * Write all records less than or equal to the specified LSN.
258 LOCK_LOGREGION(dblp);
260 /* If no LSN specified, flush the entire log. */
262 t_lsn.file = lp->lsn.file;
263 t_lsn.offset = lp->lsn.offset - lp->len;
267 /* If it's a non-existent record, it's an error. */
268 if (lsn->file > lp->lsn.file ||
269 (lsn->file == lp->lsn.file && lsn->offset > lp->lsn.offset)) {
270 __db_err(dblp->dbenv, "log_flush: LSN past current end-of-log");
276 * If it's from a previous file, we're done because we sync each
277 * file when we move to a new one.
279 if (lsn->file < lp->lsn.file)
283 * If it's less than the last-sync'd offset, we've already sync'd
286 if (lsn->offset <= lp->s_lsn.offset)
290 * We may need to write the current buffer. We have to write the
291 * current buffer if the sync LSN is greater than or equal to the
292 * saved spanning-LSN.
294 if (lsn->file >= lp->span_lsn.file &&
295 lsn->offset >= lp->span_lsn.offset)
296 if ((ret = __log_write(dblp, lp->buf, lp->b_off)) != 0)
299 /* Acquire a file descriptor if we don't have one. */
300 if (dblp->lfname != dblp->lp->lsn.file &&
301 (ret = __log_newfd(dblp)) != 0)
304 if ((ret = __db_fsync(dblp->lfd)) != 0)
307 lp->s_lsn.file = lp->lsn.file;
308 lp->s_lsn.offset = lsn->offset;
310 ret1: UNLOCK_LOGREGION(dblp);
316 * Write information into the log.
319 __log_fill(dblp, addr, len)
329 /* Copy out the data. */
330 for (lp = dblp->lp; len > 0;) {
332 * If we're on a buffer boundary and the data is big enough,
333 * copy as many records as we can directly from the data.
335 if (lp->b_off == 0 && len >= sizeof(lp->buf)) {
336 nrec = len / sizeof(lp->buf);
337 if ((ret = __log_write(dblp,
338 addr, nrec * sizeof(lp->buf))) != 0)
340 addr = (u_int8_t *)addr + nrec * sizeof(lp->buf);
341 len -= nrec * sizeof(lp->buf);
345 /* Figure out how many bytes we can copy this time. */
346 remain = sizeof(lp->buf) - lp->b_off;
347 nw = remain > len ? len : remain;
348 memcpy(lp->buf + lp->b_off, addr, nw);
349 addr = (u_int8_t *)addr + nw;
353 /* If we fill the buffer, flush it. */
354 if (lp->b_off == sizeof(lp->buf) &&
355 (ret = __log_write(dblp, lp->buf, sizeof(lp->buf))) != 0)
363 * Write the log buffer to disk.
366 __log_write(dblp, addr, len)
376 * If we haven't opened the log file yet or the current one
377 * has changed, acquire a new log file.
380 if (dblp->lfd == -1 || dblp->lfname != lp->lsn.file)
381 if ((ret = __log_newfd(dblp)) != 0)
385 * Seek to the offset in the file (someone may have written it
386 * since we last did).
388 if ((ret = __db_lseek(dblp->lfd, 0, 0, lp->w_off, SEEK_SET)) != 0)
390 if ((ret = __db_write(dblp->lfd, addr, len, &nw)) != 0)
392 if (nw != (int32_t)len)
395 /* Update the seek offset and reset the buffer offset. */
405 * Map a DB_LSN to a file name.
408 log_file(dblp, lsn, namep, len)
417 LOCK_LOGREGION(dblp);
419 ret = __log_name(dblp, lsn->file, &p);
421 UNLOCK_LOGREGION(dblp);
426 /* Check to make sure there's enough room and copy the name. */
427 if (len < strlen(p)) {
431 (void)strcpy(namep, p);
439 * Acquire a file descriptor for the current log file.
448 /* Close any previous file descriptor. */
449 if (dblp->lfd != -1) {
450 (void)__db_close(dblp->lfd);
454 /* Get the path of the new file and open it. */
455 dblp->lfname = dblp->lp->lsn.file;
456 if ((ret = __log_name(dblp, dblp->lfname, &p)) != 0)
458 if ((ret = __db_fdopen(p,
459 DB_CREATE | DB_SEQUENTIAL,
460 DB_CREATE | DB_SEQUENTIAL,
461 dblp->lp->persist.mode, &dblp->lfd)) != 0)
462 __db_err(dblp->dbenv,
463 "log_put: %s: %s", p, strerror(ret));
470 * Return the log name for a particular file.
472 * PUBLIC: int __log_name __P((DB_LOG *, int, char **));
475 __log_name(dblp, fileno, namep)
480 char name[sizeof(LFNAME) + 10];
482 (void)snprintf(name, sizeof(name), LFNAME, fileno);
483 return (__db_appname(dblp->dbenv,
484 DB_APP_LOG, dblp->dir, name, NULL, namep));