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_get.c 10.19 (Sleepycat) 9/23/97";
13 #ifndef NO_SYSTEM_INCLUDES
14 #include <sys/types.h>
28 #include "common_ext.h"
35 log_get(dblp, alsn, dbt, flags)
44 /* Validate arguments. */
45 #define OKFLAGS (DB_CHECKPOINT | \
46 DB_CURRENT | DB_FIRST | DB_LAST | DB_NEXT | DB_PREV | DB_SET)
49 __db_fchk(dblp->dbenv, "log_get", flags, OKFLAGS)) != 0)
62 return (__db_ferr(dblp->dbenv, "log_get", 1));
65 if (F_ISSET(dblp, DB_AM_THREAD)) {
66 if (LF_ISSET(DB_NEXT | DB_PREV | DB_CURRENT))
67 return (__db_ferr(dblp->dbenv, "log_get", 1));
68 if (!F_ISSET(dbt, DB_DBT_USERMEM | DB_DBT_MALLOC))
69 return (__db_ferr(dblp->dbenv, "threaded data", 1));
77 * If we get one of the log's header records, repeat the operation.
78 * This assumes that applications don't ever request the log header
79 * records by LSN, but that seems reasonable to me.
81 ret = __log_get(dblp, alsn, dbt, flags, 0);
82 if (ret == 0 && alsn->offset == 0) {
91 ret = __log_get(dblp, alsn, dbt, flags, 0);
94 UNLOCK_LOGREGION(dblp);
101 * Get a log record; internal version.
103 * PUBLIC: int __log_get __P((DB_LOG *, DB_LSN *, DBT *, int, int));
106 __log_get(dblp, alsn, dbt, flags, silent)
123 fail = np = tbuf = NULL;
128 nlsn = dblp->lp->c_lsn;
129 if (IS_ZERO_LSN(nlsn)) {
130 __db_err(dblp->dbenv,
131 "log_get: unable to find checkpoint record: no checkpoint set.");
136 case DB_NEXT: /* Next log record. */
137 if (!IS_ZERO_LSN(nlsn)) {
138 /* Increment the cursor by the cursor record size. */
139 nlsn.offset += dblp->c_len;
143 case DB_FIRST: /* Find the first log record. */
145 * Find any log file. Note, we may have only entered records
146 * in the buffer, and not yet written a log file.
148 if ((ret = __log_find(dblp, &cnt)) != 0) {
149 __db_err(dblp->dbenv,
150 "log_get: unable to find the first record: no log files found.");
154 /* If there's anything in the buffer, it belongs to file 1. */
158 /* Now go backwards to find the smallest one. */
159 for (; cnt > 1; --cnt)
160 if (__log_valid(dblp, NULL, cnt) != 0) {
167 case DB_CURRENT: /* Current log record. */
169 case DB_PREV: /* Previous log record. */
170 if (!IS_ZERO_LSN(nlsn)) {
171 /* If at start-of-file, move to the previous file. */
172 if (nlsn.offset == 0) {
174 return (DB_NOTFOUND);
177 nlsn.offset = dblp->c_off;
179 nlsn.offset = dblp->c_off;
183 case DB_LAST: /* Last log record. */
184 nlsn.file = lp->lsn.file;
185 nlsn.offset = lp->lsn.offset - lp->len;
187 case DB_SET: /* Set log record. */
193 /* Return 1 if the request is past end-of-file. */
194 if (nlsn.file > lp->lsn.file ||
195 (nlsn.file == lp->lsn.file && nlsn.offset >= lp->lsn.offset))
196 return (DB_NOTFOUND);
198 /* If we've switched files, discard the current fd. */
199 if (dblp->c_lsn.file != nlsn.file && dblp->c_fd != -1) {
200 (void)__db_close(dblp->c_fd);
204 /* If the entire record is in the in-memory buffer, copy it out. */
205 if (nlsn.file == lp->lsn.file && nlsn.offset >= lp->w_off) {
206 /* Copy the header. */
207 p = lp->buf + (nlsn.offset - lp->w_off);
208 memcpy(&hdr, p, sizeof(HDR));
210 /* Copy the record. */
211 len = hdr.len - sizeof(HDR);
212 if ((ret = __db_retcopy(dbt, (u_int8_t *)p + sizeof(HDR),
213 len, &dblp->c_dbt.data, &dblp->c_dbt.ulen, NULL)) != 0)
219 * Move the file descriptor to the page that has the hdr. We dealt
220 * with moving to a previous log file in the flags switch code, but
221 * we don't yet know if we'll need to move to a subsequent file.
223 * Acquire a file descriptor.
225 if (dblp->c_fd == -1) {
226 if ((ret = __log_name(dblp, nlsn.file, &np)) != 0)
228 if ((ret = __db_fdopen(np, DB_RDONLY | DB_SEQUENTIAL,
229 DB_RDONLY | DB_SEQUENTIAL, 0, &dblp->c_fd)) != 0) {
237 /* Seek to the header offset and read the header. */
238 if ((ret = __db_lseek(dblp->c_fd, 0, 0, nlsn.offset, SEEK_SET)) != 0) {
242 if ((ret = __db_read(dblp->c_fd, &hdr, sizeof(HDR), &nr)) != 0) {
246 if (nr == sizeof(HDR))
249 /* If read returns EOF, try the next file. */
251 if (flags != DB_NEXT || nlsn.file == lp->lsn.file)
254 /* Move to the next file. */
261 * If read returns a short count the rest of the record has
262 * to be in the in-memory buffer.
264 if (lp->b_off < sizeof(HDR) - nr)
267 /* Get the rest of the header from the in-memory buffer. */
268 memcpy((u_int8_t *)&hdr + nr, lp->buf, sizeof(HDR) - nr);
269 shortp = lp->buf + (sizeof(HDR) - nr);
273 * Check for buffers of 0's, that's what we usually see during
274 * recovery, although it's certainly not something on which we
277 if (hdr.len <= sizeof(HDR))
279 len = hdr.len - sizeof(HDR);
281 /* If we've already moved to the in-memory buffer, fill from there. */
282 if (shortp != NULL) {
283 if (lp->b_off < ((u_int8_t *)shortp - lp->buf) + len)
285 if ((ret = __db_retcopy(dbt, shortp, len,
286 &dblp->c_dbt.data, &dblp->c_dbt.ulen, NULL)) != 0)
291 /* Allocate temporary memory to hold the record. */
292 if ((tbuf = (char *)malloc(len)) == NULL) {
298 * Read the record into the buffer. If read returns a short count,
299 * there was an error or the rest of the record is in the in-memory
300 * buffer. Note, the information may be garbage if we're in recovery,
301 * so don't read past the end of the buffer's memory.
303 if ((ret = __db_read(dblp->c_fd, tbuf, len, &nr)) != 0) {
307 if (len - nr > sizeof(lp->buf))
309 if (nr != (ssize_t)len) {
310 if (lp->b_off < len - nr)
313 /* Get the rest of the record from the in-memory buffer. */
314 memcpy((u_int8_t *)tbuf + nr, lp->buf, len - nr);
317 /* Copy the record into the user's DBT. */
318 if ((ret = __db_retcopy(dbt, tbuf, len,
319 &dblp->c_dbt.data, &dblp->c_dbt.ulen, NULL)) != 0)
324 cksum: if (hdr.cksum != __ham_func4(dbt->data, dbt->size)) {
326 __db_err(dblp->dbenv, "log_get: checksum mismatch");
330 /* Update the cursor and the return lsn. */
331 dblp->c_off = hdr.prev;
332 dblp->c_len = hdr.len;
333 dblp->c_lsn = *alsn = nlsn;
338 * This is the catchall -- for some reason we didn't find enough
339 * information or it wasn't reasonable information, and it wasn't
340 * because a system call failed.
347 __db_err(dblp->dbenv, "log_get: %s", strerror(ret));
349 __db_err(dblp->dbenv,
350 "log_get: %s: %s", fail, strerror(ret));
351 err2: if (np != NULL)