xref: /illumos-gate/usr/src/cmd/sendmail/db/log/log_rec.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*-
27c478bd9Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
57c478bd9Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
67c478bd9Sstevel@tonic-gate  */
77c478bd9Sstevel@tonic-gate /*
87c478bd9Sstevel@tonic-gate  * Copyright (c) 1995, 1996
97c478bd9Sstevel@tonic-gate  *	The President and Fellows of Harvard University.  All rights reserved.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
127c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
137c478bd9Sstevel@tonic-gate  * are met:
147c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
157c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
167c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
177c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
187c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
197c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
207c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
217c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
227c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
237c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
247c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
257c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
267c478bd9Sstevel@tonic-gate  *
277c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
287c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
297c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
307c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
317c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
327c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
337c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
347c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
357c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
367c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
377c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "config.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #ifndef lint
437c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)log_rec.c	10.26 (Sleepycat) 10/21/98";
447c478bd9Sstevel@tonic-gate #endif /* not lint */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
477c478bd9Sstevel@tonic-gate #include <sys/types.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #include <errno.h>
507c478bd9Sstevel@tonic-gate #include <string.h>
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "db_int.h"
547c478bd9Sstevel@tonic-gate #include "shqueue.h"
557c478bd9Sstevel@tonic-gate #include "log.h"
567c478bd9Sstevel@tonic-gate #include "db_dispatch.h"
577c478bd9Sstevel@tonic-gate #include "common_ext.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static int __log_do_open __P((DB_LOG *,
607c478bd9Sstevel@tonic-gate     u_int8_t *, char *, DBTYPE, u_int32_t));
617c478bd9Sstevel@tonic-gate static int __log_lid_to_fname __P((DB_LOG *, u_int32_t, FNAME **));
627c478bd9Sstevel@tonic-gate static int __log_open_file __P((DB_LOG *, __log_register_args *));
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
657c478bd9Sstevel@tonic-gate  * PUBLIC: int __log_register_recover
667c478bd9Sstevel@tonic-gate  * PUBLIC:     __P((DB_LOG *, DBT *, DB_LSN *, int, void *));
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate int
__log_register_recover(logp,dbtp,lsnp,redo,info)697c478bd9Sstevel@tonic-gate __log_register_recover(logp, dbtp, lsnp, redo, info)
707c478bd9Sstevel@tonic-gate 	DB_LOG *logp;
717c478bd9Sstevel@tonic-gate 	DBT *dbtp;
727c478bd9Sstevel@tonic-gate 	DB_LSN *lsnp;
737c478bd9Sstevel@tonic-gate 	int redo;
747c478bd9Sstevel@tonic-gate 	void *info;
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	DB_ENTRY *dbe;
777c478bd9Sstevel@tonic-gate 	__log_register_args *argp;
787c478bd9Sstevel@tonic-gate 	int ret;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #ifdef DEBUG_RECOVER
817c478bd9Sstevel@tonic-gate 	__log_register_print(logp, dbtp, lsnp, redo, info);
827c478bd9Sstevel@tonic-gate #endif
837c478bd9Sstevel@tonic-gate 	COMPQUIET(info, NULL);
847c478bd9Sstevel@tonic-gate 	COMPQUIET(lsnp, NULL);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	F_SET(logp, DBC_RECOVER);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if ((ret = __log_register_read(dbtp->data, &argp)) != 0)
897c478bd9Sstevel@tonic-gate 		goto out;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if ((argp->opcode == LOG_CHECKPOINT && redo == TXN_OPENFILES) ||
927c478bd9Sstevel@tonic-gate 	    (argp->opcode == LOG_OPEN &&
937c478bd9Sstevel@tonic-gate 	    (redo == TXN_REDO || redo == TXN_OPENFILES ||
947c478bd9Sstevel@tonic-gate 	     redo == TXN_FORWARD_ROLL)) ||
957c478bd9Sstevel@tonic-gate 	    (argp->opcode == LOG_CLOSE &&
967c478bd9Sstevel@tonic-gate 	    (redo == TXN_UNDO || redo == TXN_BACKWARD_ROLL))) {
977c478bd9Sstevel@tonic-gate 		/*
987c478bd9Sstevel@tonic-gate 		 * If we are redoing an open or undoing a close, then we need
997c478bd9Sstevel@tonic-gate 		 * to open a file.
1007c478bd9Sstevel@tonic-gate 		 */
1017c478bd9Sstevel@tonic-gate 		ret = __log_open_file(logp, argp);
1027c478bd9Sstevel@tonic-gate 		if (ret == ENOENT) {
1037c478bd9Sstevel@tonic-gate 			if (redo == TXN_OPENFILES)
1047c478bd9Sstevel@tonic-gate 				__db_err(logp->dbenv, "warning: %s: %s",
1057c478bd9Sstevel@tonic-gate 				    argp->name.data, strerror(ENOENT));
1067c478bd9Sstevel@tonic-gate 			ret = 0;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 	} else if (argp->opcode != LOG_CHECKPOINT &&
1097c478bd9Sstevel@tonic-gate 	    argp->opcode != LOG_CLOSE) {
1107c478bd9Sstevel@tonic-gate 		/*
1117c478bd9Sstevel@tonic-gate 		 * If we are undoing an open, then we need to close the file.
1127c478bd9Sstevel@tonic-gate 		 * Note that we do *not* close the file if we are redoing a
1137c478bd9Sstevel@tonic-gate 		 * close, because we do not log the reference counts on log
1147c478bd9Sstevel@tonic-gate 		 * files and we may have had the file open multiple times,
1157c478bd9Sstevel@tonic-gate 		 * and therefore, this close should just dec a reference
1167c478bd9Sstevel@tonic-gate 		 * count.  However, since we only do one open during a
1177c478bd9Sstevel@tonic-gate 		 * checkpoint, this will inadvertently close the file.
1187c478bd9Sstevel@tonic-gate 		 *
1197c478bd9Sstevel@tonic-gate   		 * If the file is deleted, then we can just ignore this close.
1207c478bd9Sstevel@tonic-gate  		 * Otherwise, we should usually have a valid dbp we should
1217c478bd9Sstevel@tonic-gate   		 * close or whose reference count should be decremented.
1227c478bd9Sstevel@tonic-gate  		 * However, if we shut down without closing a file, we
1237c478bd9Sstevel@tonic-gate 		 * may, in fact, not have the file open, and that's OK.
1247c478bd9Sstevel@tonic-gate 		 */
1257c478bd9Sstevel@tonic-gate 		LOCK_LOGTHREAD(logp);
1267c478bd9Sstevel@tonic-gate 		if (argp->id < logp->dbentry_cnt) {
1277c478bd9Sstevel@tonic-gate 			dbe = &logp->dbentry[argp->id];
1287c478bd9Sstevel@tonic-gate 			if (dbe->dbp != NULL && --dbe->refcount == 0) {
1297c478bd9Sstevel@tonic-gate 				ret = dbe->dbp->close(dbe->dbp, 0);
1307c478bd9Sstevel@tonic-gate 				if (dbe->name != NULL) {
1317c478bd9Sstevel@tonic-gate 					__os_freestr(dbe->name);
1327c478bd9Sstevel@tonic-gate 					dbe->name = NULL;
1337c478bd9Sstevel@tonic-gate 				}
1347c478bd9Sstevel@tonic-gate 				(void)__log_rem_logid(logp, argp->id);
1357c478bd9Sstevel@tonic-gate 			}
1367c478bd9Sstevel@tonic-gate 		}
1377c478bd9Sstevel@tonic-gate 		UNLOCK_LOGTHREAD(logp);
1387c478bd9Sstevel@tonic-gate  	} else if (argp->opcode == LOG_CHECKPOINT && redo == TXN_UNDO &&
1397c478bd9Sstevel@tonic-gate  	    (argp->id >= logp->dbentry_cnt ||
1407c478bd9Sstevel@tonic-gate  	    (!logp->dbentry[argp->id].deleted &&
1417c478bd9Sstevel@tonic-gate  	    logp->dbentry[argp->id].dbp == NULL))) {
1427c478bd9Sstevel@tonic-gate  		/*
1437c478bd9Sstevel@tonic-gate  		 * It's a checkpoint and we are rolling backward.  It
1447c478bd9Sstevel@tonic-gate  		 * is possible that the system was shut down and thus
1457c478bd9Sstevel@tonic-gate  		 * ended with a stable checkpoint; this file was never
1467c478bd9Sstevel@tonic-gate  		 * closed and has therefore not been reopened yet.  If
1477c478bd9Sstevel@tonic-gate  		 * so, we need to try to open it.
1487c478bd9Sstevel@tonic-gate  		 */
1497c478bd9Sstevel@tonic-gate  		ret = __log_open_file(logp, argp);
1507c478bd9Sstevel@tonic-gate  		if (ret == ENOENT) {
1517c478bd9Sstevel@tonic-gate  			__db_err(logp->dbenv, "warning: %s: %s",
1527c478bd9Sstevel@tonic-gate 			    argp->name.data, strerror(ENOENT));
1537c478bd9Sstevel@tonic-gate  			ret = 0;
1547c478bd9Sstevel@tonic-gate  		}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate out:	F_CLR(logp, DBC_RECOVER);
1587c478bd9Sstevel@tonic-gate 	if (argp != NULL)
1597c478bd9Sstevel@tonic-gate 		__os_free(argp, 0);
1607c478bd9Sstevel@tonic-gate 	return (ret);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /* Hand coded routines. */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Called during log_register recovery.  Make sure that we have an
1677c478bd9Sstevel@tonic-gate  * entry in the dbentry table for this ndx.
1687c478bd9Sstevel@tonic-gate  * Returns 0 on success, non-zero on error.
1697c478bd9Sstevel@tonic-gate  */
1707c478bd9Sstevel@tonic-gate static int
__log_open_file(lp,argp)1717c478bd9Sstevel@tonic-gate __log_open_file(lp, argp)
1727c478bd9Sstevel@tonic-gate 	DB_LOG *lp;
1737c478bd9Sstevel@tonic-gate 	__log_register_args *argp;
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	DB_ENTRY *dbe;
176*55fea89dSDan Cross 
1777c478bd9Sstevel@tonic-gate 	if (argp->name.size == 0)
1787c478bd9Sstevel@tonic-gate 		return(0);
179*55fea89dSDan Cross 
1807c478bd9Sstevel@tonic-gate 	/*
1817c478bd9Sstevel@tonic-gate 	 * Because of reference counting, we cannot automatically close files
1827c478bd9Sstevel@tonic-gate 	 * during recovery, so when we're opening, we have to check that the
1837c478bd9Sstevel@tonic-gate 	 * name we are opening is what we expect.  If it's not, then we close
1847c478bd9Sstevel@tonic-gate 	 * the old file and open the new one.
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	LOCK_LOGTHREAD(lp);
1877c478bd9Sstevel@tonic-gate 	if (argp->id < lp->dbentry_cnt)
1887c478bd9Sstevel@tonic-gate 		dbe = &lp->dbentry[argp->id];
1897c478bd9Sstevel@tonic-gate 	else
1907c478bd9Sstevel@tonic-gate 		dbe = NULL;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (dbe != NULL && (dbe->deleted == 1 || dbe->dbp != NULL) &&
1937c478bd9Sstevel@tonic-gate 	    dbe->name != NULL && argp->name.data != NULL &&
1947c478bd9Sstevel@tonic-gate 	    strncmp(argp->name.data, dbe->name, argp->name.size) == 0) {
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		dbe->refcount++;
1977c478bd9Sstevel@tonic-gate 		UNLOCK_LOGTHREAD(lp);
1987c478bd9Sstevel@tonic-gate 		return (0);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate   	UNLOCK_LOGTHREAD(lp);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (dbe != NULL && dbe->dbp != NULL) {
2037c478bd9Sstevel@tonic-gate                 (void)dbe->dbp->close(dbe->dbp, 0);
2047c478bd9Sstevel@tonic-gate 		if (dbe->name != NULL)
2057c478bd9Sstevel@tonic-gate 			__os_freestr(dbe->name);
2067c478bd9Sstevel@tonic-gate                 dbe->name = NULL;
2077c478bd9Sstevel@tonic-gate 		(void)__log_rem_logid(lp, argp->id);
2087c478bd9Sstevel@tonic-gate         }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	return (__log_do_open(lp,
2127c478bd9Sstevel@tonic-gate 	    argp->uid.data, argp->name.data, argp->ftype, argp->id));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * __log_do_open --
2177c478bd9Sstevel@tonic-gate  * 	Open files referenced in the log.  This is the part of the open that
2187c478bd9Sstevel@tonic-gate  * is not protected by the thread mutex.
2197c478bd9Sstevel@tonic-gate  */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate static int
__log_do_open(lp,uid,name,ftype,ndx)2227c478bd9Sstevel@tonic-gate __log_do_open(lp, uid, name, ftype, ndx)
2237c478bd9Sstevel@tonic-gate 	DB_LOG *lp;
2247c478bd9Sstevel@tonic-gate 	u_int8_t *uid;
2257c478bd9Sstevel@tonic-gate 	char *name;
2267c478bd9Sstevel@tonic-gate 	DBTYPE ftype;
2277c478bd9Sstevel@tonic-gate 	u_int32_t ndx;
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	DB *dbp;
2307c478bd9Sstevel@tonic-gate 	int ret;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	dbp = NULL;
2337c478bd9Sstevel@tonic-gate 	if ((ret = db_open(name, ftype, 0, 0, lp->dbenv, NULL, &dbp)) == 0) {
2347c478bd9Sstevel@tonic-gate 		/*
2357c478bd9Sstevel@tonic-gate 		 * Verify that we are opening the same file that we were
2367c478bd9Sstevel@tonic-gate 		 * referring to when we wrote this log record.
2377c478bd9Sstevel@tonic-gate 		 */
2387c478bd9Sstevel@tonic-gate 		if (memcmp(uid, dbp->fileid, DB_FILE_ID_LEN) != 0) {
2397c478bd9Sstevel@tonic-gate 			(void)dbp->close(dbp, 0);
2407c478bd9Sstevel@tonic-gate 			dbp = NULL;
2417c478bd9Sstevel@tonic-gate 			ret = ENOENT;
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (ret == 0 || ret == ENOENT)
2467c478bd9Sstevel@tonic-gate 		(void)__log_add_logid(lp, dbp, name, ndx);
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	return (ret);
2497c478bd9Sstevel@tonic-gate }
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * __log_add_logid --
2537c478bd9Sstevel@tonic-gate  *	Adds a DB entry to the log's DB entry table.
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * PUBLIC: int __log_add_logid __P((DB_LOG *, DB *, const char *, u_int32_t));
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate int
__log_add_logid(logp,dbp,name,ndx)2587c478bd9Sstevel@tonic-gate __log_add_logid(logp, dbp, name, ndx)
2597c478bd9Sstevel@tonic-gate 	DB_LOG *logp;
2607c478bd9Sstevel@tonic-gate 	DB *dbp;
2617c478bd9Sstevel@tonic-gate 	const char *name;
2627c478bd9Sstevel@tonic-gate 	u_int32_t ndx;
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate 	u_int32_t i;
2657c478bd9Sstevel@tonic-gate 	int ret;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	ret = 0;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	LOCK_LOGTHREAD(logp);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	/*
2727c478bd9Sstevel@tonic-gate 	 * Check if we need to grow the table.  Note, ndx is 0-based (the
2737c478bd9Sstevel@tonic-gate 	 * index into the DB entry table) an dbentry_cnt is 1-based, the
2747c478bd9Sstevel@tonic-gate 	 * number of available slots.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	if (logp->dbentry_cnt <= ndx) {
2777c478bd9Sstevel@tonic-gate 		if ((ret = __os_realloc(&logp->dbentry,
2787c478bd9Sstevel@tonic-gate 		    (ndx + DB_GROW_SIZE) * sizeof(DB_ENTRY))) != 0)
2797c478bd9Sstevel@tonic-gate 			goto err;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 		/* Initialize the new entries. */
2827c478bd9Sstevel@tonic-gate 		for (i = logp->dbentry_cnt; i < ndx + DB_GROW_SIZE; i++) {
2837c478bd9Sstevel@tonic-gate 			logp->dbentry[i].dbp = NULL;
2847c478bd9Sstevel@tonic-gate 			logp->dbentry[i].deleted = 0;
2857c478bd9Sstevel@tonic-gate 			logp->dbentry[i].name = NULL;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		logp->dbentry_cnt = i;
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* Make space for the name and copy it in. */
2927c478bd9Sstevel@tonic-gate 	if (name != NULL) {
293*55fea89dSDan Cross 		if ((ret = __os_malloc(strlen(name) + 1,
2947c478bd9Sstevel@tonic-gate 		    NULL, &logp->dbentry[ndx].name)) != 0)
2957c478bd9Sstevel@tonic-gate 			goto err;
2967c478bd9Sstevel@tonic-gate 		strcpy(logp->dbentry[ndx].name, name);
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	if (logp->dbentry[ndx].deleted == 0 && logp->dbentry[ndx].dbp == NULL) {
3007c478bd9Sstevel@tonic-gate 		logp->dbentry[ndx].dbp = dbp;
3017c478bd9Sstevel@tonic-gate 		logp->dbentry[ndx].refcount = 1;
3027c478bd9Sstevel@tonic-gate 		logp->dbentry[ndx].deleted = dbp == NULL;
3037c478bd9Sstevel@tonic-gate 	} else
3047c478bd9Sstevel@tonic-gate 		logp->dbentry[ndx].refcount++;
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate err:	UNLOCK_LOGTHREAD(logp);
3087c478bd9Sstevel@tonic-gate 	return (ret);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * __db_fileid_to_db --
3147c478bd9Sstevel@tonic-gate  *	Return the DB corresponding to the specified fileid.
3157c478bd9Sstevel@tonic-gate  *
3167c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_fileid_to_db __P((DB_LOG *, DB **, u_int32_t));
3177c478bd9Sstevel@tonic-gate  */
3187c478bd9Sstevel@tonic-gate int
__db_fileid_to_db(logp,dbpp,ndx)3197c478bd9Sstevel@tonic-gate __db_fileid_to_db(logp, dbpp, ndx)
3207c478bd9Sstevel@tonic-gate 	DB_LOG *logp;
3217c478bd9Sstevel@tonic-gate 	DB **dbpp;
3227c478bd9Sstevel@tonic-gate 	u_int32_t ndx;
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	int ret;
3257c478bd9Sstevel@tonic-gate 	char *name;
3267c478bd9Sstevel@tonic-gate 	FNAME *fname;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	ret = 0;
3297c478bd9Sstevel@tonic-gate 	LOCK_LOGTHREAD(logp);
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/*
3327c478bd9Sstevel@tonic-gate 	 * Under XA, a process different than the one issuing DB
333*55fea89dSDan Cross 	 * operations may abort a transaction.  In this case,
3347c478bd9Sstevel@tonic-gate 	 * recovery routines are run by a process that does not
3357c478bd9Sstevel@tonic-gate 	 * necessarily have the file open.  In this case, we must
3367c478bd9Sstevel@tonic-gate 	 * open the file explicitly.
3377c478bd9Sstevel@tonic-gate 	 */
3387c478bd9Sstevel@tonic-gate 	if (ndx >= logp->dbentry_cnt ||
3397c478bd9Sstevel@tonic-gate 	    (!logp->dbentry[ndx].deleted && logp->dbentry[ndx].dbp == NULL)) {
3407c478bd9Sstevel@tonic-gate 		if (__log_lid_to_fname(logp, ndx, &fname) != 0) {
3417c478bd9Sstevel@tonic-gate 			/* Couldn't find entry; this is a fatal error. */
3427c478bd9Sstevel@tonic-gate 			ret = EINVAL;
3437c478bd9Sstevel@tonic-gate 			goto err;
3447c478bd9Sstevel@tonic-gate 		}
3457c478bd9Sstevel@tonic-gate 		name = R_ADDR(logp, fname->name_off);
3467c478bd9Sstevel@tonic-gate 		/*
3477c478bd9Sstevel@tonic-gate 		 * __log_do_open is called without protection of the
3487c478bd9Sstevel@tonic-gate 		 * log thread lock.
3497c478bd9Sstevel@tonic-gate 		 */
3507c478bd9Sstevel@tonic-gate 		UNLOCK_LOGTHREAD(logp);
3517c478bd9Sstevel@tonic-gate 		/*
3527c478bd9Sstevel@tonic-gate 		 * At this point, we are not holding the thread lock, so
3537c478bd9Sstevel@tonic-gate 		 * exit directly instead of going through the exit code
3547c478bd9Sstevel@tonic-gate 		 * at the bottom.  If the __log_do_open succeeded, then
3557c478bd9Sstevel@tonic-gate 		 * we don't need to do any of the remaining error checking
3567c478bd9Sstevel@tonic-gate 		 * at the end of this routine.
3577c478bd9Sstevel@tonic-gate 		 */
3587c478bd9Sstevel@tonic-gate 		if ((ret = __log_do_open(logp,
3597c478bd9Sstevel@tonic-gate 		    fname->ufid, name, fname->s_type, ndx)) != 0)
3607c478bd9Sstevel@tonic-gate 			return (ret);
3617c478bd9Sstevel@tonic-gate 		*dbpp = logp->dbentry[ndx].dbp;
3627c478bd9Sstevel@tonic-gate 		return (0);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 	/*
3667c478bd9Sstevel@tonic-gate 	 * Return DB_DELETED if the file has been deleted
3677c478bd9Sstevel@tonic-gate 	 * (it's not an error).
3687c478bd9Sstevel@tonic-gate 	 */
3697c478bd9Sstevel@tonic-gate 	if (logp->dbentry[ndx].deleted) {
3707c478bd9Sstevel@tonic-gate 		ret = DB_DELETED;
3717c478bd9Sstevel@tonic-gate 		goto err;
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	/*
3757c478bd9Sstevel@tonic-gate 	 * Otherwise return 0, but if we don't have a corresponding DB,
3767c478bd9Sstevel@tonic-gate 	 * it's an error.
3777c478bd9Sstevel@tonic-gate 	 */
3787c478bd9Sstevel@tonic-gate 	if ((*dbpp = logp->dbentry[ndx].dbp) == NULL)
3797c478bd9Sstevel@tonic-gate 		ret = ENOENT;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate err:	UNLOCK_LOGTHREAD(logp);
3827c478bd9Sstevel@tonic-gate 	return (ret);
3837c478bd9Sstevel@tonic-gate }
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate  * Close files that were opened by the recovery daemon.
3877c478bd9Sstevel@tonic-gate  *
3887c478bd9Sstevel@tonic-gate  * PUBLIC: void __log_close_files __P((DB_LOG *));
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate void
__log_close_files(logp)3917c478bd9Sstevel@tonic-gate __log_close_files(logp)
3927c478bd9Sstevel@tonic-gate 	DB_LOG *logp;
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	u_int32_t i;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	LOCK_LOGTHREAD(logp);
3977c478bd9Sstevel@tonic-gate 	for (i = 0; i < logp->dbentry_cnt; i++)
3987c478bd9Sstevel@tonic-gate 		if (logp->dbentry[i].dbp) {
3997c478bd9Sstevel@tonic-gate 			logp->dbentry[i].dbp->close(logp->dbentry[i].dbp, 0);
4007c478bd9Sstevel@tonic-gate 			logp->dbentry[i].dbp = NULL;
4017c478bd9Sstevel@tonic-gate 			logp->dbentry[i].deleted = 0;
4027c478bd9Sstevel@tonic-gate 		}
4037c478bd9Sstevel@tonic-gate 	F_CLR(logp, DBC_RECOVER);
4047c478bd9Sstevel@tonic-gate 	UNLOCK_LOGTHREAD(logp);
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
4087c478bd9Sstevel@tonic-gate  * PUBLIC: void __log_rem_logid __P((DB_LOG *, u_int32_t));
4097c478bd9Sstevel@tonic-gate  */
4107c478bd9Sstevel@tonic-gate void
__log_rem_logid(logp,ndx)4117c478bd9Sstevel@tonic-gate __log_rem_logid(logp, ndx)
4127c478bd9Sstevel@tonic-gate 	DB_LOG *logp;
4137c478bd9Sstevel@tonic-gate 	u_int32_t ndx;
4147c478bd9Sstevel@tonic-gate {
4157c478bd9Sstevel@tonic-gate 	LOCK_LOGTHREAD(logp);
4167c478bd9Sstevel@tonic-gate 	if (--logp->dbentry[ndx].refcount == 0) {
4177c478bd9Sstevel@tonic-gate 		logp->dbentry[ndx].dbp = NULL;
4187c478bd9Sstevel@tonic-gate 		logp->dbentry[ndx].deleted = 0;
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	UNLOCK_LOGTHREAD(logp);
4217c478bd9Sstevel@tonic-gate }
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * __log_lid_to_fname --
4257c478bd9Sstevel@tonic-gate  * 	Traverse the shared-memory region looking for the entry that
4267c478bd9Sstevel@tonic-gate  * matches the passed log fileid.  Returns 0 on success; -1 on error.
4277c478bd9Sstevel@tonic-gate  */
4287c478bd9Sstevel@tonic-gate static int
__log_lid_to_fname(dblp,lid,fnamep)4297c478bd9Sstevel@tonic-gate __log_lid_to_fname(dblp, lid, fnamep)
4307c478bd9Sstevel@tonic-gate 	DB_LOG *dblp;
4317c478bd9Sstevel@tonic-gate 	u_int32_t lid;
4327c478bd9Sstevel@tonic-gate 	FNAME **fnamep;
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	FNAME *fnp;
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	for (fnp = SH_TAILQ_FIRST(&dblp->lp->fq, __fname);
4377c478bd9Sstevel@tonic-gate 	    fnp != NULL; fnp = SH_TAILQ_NEXT(fnp, q, __fname)) {
4387c478bd9Sstevel@tonic-gate 		if (fnp->ref == 0)	/* Entry not in use. */
4397c478bd9Sstevel@tonic-gate 			continue;
4407c478bd9Sstevel@tonic-gate 		if (fnp->id == lid) {
4417c478bd9Sstevel@tonic-gate 			*fnamep = fnp;
4427c478bd9Sstevel@tonic-gate 			return (0);
4437c478bd9Sstevel@tonic-gate 		}
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	return (-1);
4467c478bd9Sstevel@tonic-gate }
447