17c478bdstevel@tonic-gate/*-
27c478bdstevel@tonic-gate * See the file LICENSE for redistribution information.
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * Copyright (c) 1998
57c478bdstevel@tonic-gate *	Sleepycat Software.  All rights reserved.
67c478bdstevel@tonic-gate */
77c478bdstevel@tonic-gate
87c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
97c478bdstevel@tonic-gate
107c478bdstevel@tonic-gate#include "config.h"
117c478bdstevel@tonic-gate
127c478bdstevel@tonic-gate#ifndef lint
137c478bdstevel@tonic-gatestatic const char sccsid[] = "@(#)db_am.c	10.15 (Sleepycat) 12/30/98";
147c478bdstevel@tonic-gate#endif /* not lint */
157c478bdstevel@tonic-gate
167c478bdstevel@tonic-gate#ifndef NO_SYSTEM_INCLUDES
177c478bdstevel@tonic-gate#include <sys/types.h>
187c478bdstevel@tonic-gate
197c478bdstevel@tonic-gate#include <errno.h>
207c478bdstevel@tonic-gate#include <stdlib.h>
217c478bdstevel@tonic-gate#include <string.h>
227c478bdstevel@tonic-gate#endif
237c478bdstevel@tonic-gate
247c478bdstevel@tonic-gate#include "db_int.h"
257c478bdstevel@tonic-gate#include "shqueue.h"
267c478bdstevel@tonic-gate#include "db_page.h"
277c478bdstevel@tonic-gate#include "db_shash.h"
287c478bdstevel@tonic-gate#include "mp.h"
297c478bdstevel@tonic-gate#include "btree.h"
307c478bdstevel@tonic-gate#include "hash.h"
317c478bdstevel@tonic-gate#include "db_am.h"
327c478bdstevel@tonic-gate#include "db_ext.h"
337c478bdstevel@tonic-gate
347c478bdstevel@tonic-gatestatic int __db_c_close __P((DBC *));
357c478bdstevel@tonic-gatestatic int __db_cursor __P((DB *, DB_TXN *, DBC **, u_int32_t));
367c478bdstevel@tonic-gatestatic int __db_fd __P((DB *, int *));
377c478bdstevel@tonic-gatestatic int __db_get __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
387c478bdstevel@tonic-gatestatic int __db_put __P((DB *, DB_TXN *, DBT *, DBT *, u_int32_t));
397c478bdstevel@tonic-gate
407c478bdstevel@tonic-gate/*
417c478bdstevel@tonic-gate * __db_init_wrapper --
427c478bdstevel@tonic-gate *	Wrapper layer to implement generic DB functions.
437c478bdstevel@tonic-gate *
447c478bdstevel@tonic-gate * PUBLIC: int __db_init_wrapper __P((DB *));
457c478bdstevel@tonic-gate */
467c478bdstevel@tonic-gateint
477c478bdstevel@tonic-gate__db_init_wrapper(dbp)
487c478bdstevel@tonic-gate	DB *dbp;
497c478bdstevel@tonic-gate{
507c478bdstevel@tonic-gate	dbp->close = __db_close;
517c478bdstevel@tonic-gate	dbp->cursor = __db_cursor;
527c478bdstevel@tonic-gate	dbp->del = NULL;		/* !!! Must be set by access method. */
537c478bdstevel@tonic-gate	dbp->fd = __db_fd;
547c478bdstevel@tonic-gate	dbp->get = __db_get;
557c478bdstevel@tonic-gate	dbp->join = __db_join;
567c478bdstevel@tonic-gate	dbp->put = __db_put;
577c478bdstevel@tonic-gate	dbp->stat = NULL;		/* !!! Must be set by access method. */
587c478bdstevel@tonic-gate	dbp->sync = __db_sync;
597c478bdstevel@tonic-gate
607c478bdstevel@tonic-gate	return (0);
617c478bdstevel@tonic-gate}
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate/*
647c478bdstevel@tonic-gate * __db_cursor --
657c478bdstevel@tonic-gate *	Allocate and return a cursor.
667c478bdstevel@tonic-gate */
677c478bdstevel@tonic-gatestatic int
687c478bdstevel@tonic-gate__db_cursor(dbp, txn, dbcp, flags)
697c478bdstevel@tonic-gate	DB *dbp;
707c478bdstevel@tonic-gate	DB_TXN *txn;
717c478bdstevel@tonic-gate	DBC **dbcp;
727c478bdstevel@tonic-gate	u_int32_t flags;
737c478bdstevel@tonic-gate{
747c478bdstevel@tonic-gate	DBC *dbc, *adbc;
757c478bdstevel@tonic-gate	int ret;
767c478bdstevel@tonic-gate	db_lockmode_t mode;
777c478bdstevel@tonic-gate	u_int32_t op;
787c478bdstevel@tonic-gate
797c478bdstevel@tonic-gate	DB_PANIC_CHECK(dbp);
807c478bdstevel@tonic-gate
817c478bdstevel@tonic-gate	/* Take one from the free list if it's available. */
827c478bdstevel@tonic-gate	DB_THREAD_LOCK(dbp);
837c478bdstevel@tonic-gate	if ((dbc = TAILQ_FIRST(&dbp->free_queue)) != NULL)
847c478bdstevel@tonic-gate		TAILQ_REMOVE(&dbp->free_queue, dbc, links);
857c478bdstevel@tonic-gate	else {
867c478bdstevel@tonic-gate		DB_THREAD_UNLOCK(dbp);
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate		if ((ret = __os_calloc(1, sizeof(DBC), &dbc)) != 0)
897c478bdstevel@tonic-gate			return (ret);
907c478bdstevel@tonic-gate
917c478bdstevel@tonic-gate		dbc->dbp = dbp;
927c478bdstevel@tonic-gate		dbc->c_close = __db_c_close;
937c478bdstevel@tonic-gate
947c478bdstevel@tonic-gate		/* Set up locking information. */
957c478bdstevel@tonic-gate		if (F_ISSET(dbp, DB_AM_LOCKING | DB_AM_CDB)) {
967c478bdstevel@tonic-gate 			/*
977c478bdstevel@tonic-gate 			 * If we are not threaded, then there is no need to
987c478bdstevel@tonic-gate 			 * create new locker ids.  We know that no one else
997c478bdstevel@tonic-gate 			 * is running concurrently using this DB, so we can
1007c478bdstevel@tonic-gate 			 * take a peek at any cursors on the active queue.
1017c478bdstevel@tonic-gate 			 */
1027c478bdstevel@tonic-gate 			if (!F_ISSET(dbp, DB_AM_THREAD) &&
1037c478bdstevel@tonic-gate 			    (adbc = TAILQ_FIRST(&dbp->active_queue)) != NULL)
1047c478bdstevel@tonic-gate 				dbc->lid = adbc->lid;
1057c478bdstevel@tonic-gate 			else
1067c478bdstevel@tonic-gate 				if ((ret = lock_id(dbp->dbenv->lk_info,
1077c478bdstevel@tonic-gate 				    &dbc->lid)) != 0)
1087c478bdstevel@tonic-gate 					goto err;
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate			memcpy(dbc->lock.fileid, dbp->fileid, DB_FILE_ID_LEN);
1117c478bdstevel@tonic-gate			if (F_ISSET(dbp, DB_AM_CDB)) {
1127c478bdstevel@tonic-gate				dbc->lock_dbt.size = DB_FILE_ID_LEN;
1137c478bdstevel@tonic-gate				dbc->lock_dbt.data = dbc->lock.fileid;
1147c478bdstevel@tonic-gate			} else {
1157c478bdstevel@tonic-gate				dbc->lock_dbt.size = sizeof(dbc->lock);
1167c478bdstevel@tonic-gate				dbc->lock_dbt.data = &dbc->lock;
1177c478bdstevel@tonic-gate			}
1187c478bdstevel@tonic-gate		}
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate		switch (dbp->type) {
1217c478bdstevel@tonic-gate		case DB_BTREE:
1227c478bdstevel@tonic-gate		case DB_RECNO:
1237c478bdstevel@tonic-gate			if ((ret = __bam_c_init(dbc)) != 0)
1247c478bdstevel@tonic-gate				goto err;
1257c478bdstevel@tonic-gate			break;
1267c478bdstevel@tonic-gate		case DB_HASH:
1277c478bdstevel@tonic-gate			if ((ret = __ham_c_init(dbc)) != 0)
1287c478bdstevel@tonic-gate				goto err;
1297c478bdstevel@tonic-gate			break;
1307c478bdstevel@tonic-gate		default:
1317c478bdstevel@tonic-gate			ret = EINVAL;
1327c478bdstevel@tonic-gate			goto err;
1337c478bdstevel@tonic-gate		}
1347c478bdstevel@tonic-gate
1357c478bdstevel@tonic-gate		DB_THREAD_LOCK(dbp);
1367c478bdstevel@tonic-gate	}
1377c478bdstevel@tonic-gate
1387c478bdstevel@tonic-gate	if ((dbc->txn = txn) == NULL)
1397c478bdstevel@tonic-gate		dbc->locker = dbc->lid;
1407c478bdstevel@tonic-gate	else
1417c478bdstevel@tonic-gate		dbc->locker = txn->txnid;
1427c478bdstevel@tonic-gate
1437c478bdstevel@tonic-gate	TAILQ_INSERT_TAIL(&dbp->active_queue, dbc, links);
1447c478bdstevel@tonic-gate	DB_THREAD_UNLOCK(dbp);
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gate	/*
1477c478bdstevel@tonic-gate	 * If this is the concurrent DB product, then we do all locking
1487c478bdstevel@tonic-gate	 * in the interface, which is right here.
1497c478bdstevel@tonic-gate	 */
1507c478bdstevel@tonic-gate	if (F_ISSET(dbp, DB_AM_CDB)) {
1517c478bdstevel@tonic-gate		op = LF_ISSET(DB_OPFLAGS_MASK);
1527c478bdstevel@tonic-gate		mode = (op == DB_WRITELOCK) ? DB_LOCK_WRITE :
1537c478bdstevel@tonic-gate		    (LF_ISSET(DB_RMW) ? DB_LOCK_IWRITE : DB_LOCK_READ);
1547c478bdstevel@tonic-gate		if ((ret = lock_get(dbp->dbenv->lk_info, dbc->locker, 0,
1557c478bdstevel@tonic-gate		    &dbc->lock_dbt, mode, &dbc->mylock)) != 0) {
1567c478bdstevel@tonic-gate			(void)__db_c_close(dbc);
1577c478bdstevel@tonic-gate			return (EAGAIN);
1587c478bdstevel@tonic-gate		}
1597c478bdstevel@tonic-gate		if (LF_ISSET(DB_RMW))
1607c478bdstevel@tonic-gate			F_SET(dbc, DBC_RMW);
1617c478bdstevel@tonic-gate		if (op == DB_WRITELOCK)
1627c478bdstevel@tonic-gate			F_SET(dbc, DBC_WRITER);
1637c478bdstevel@tonic-gate	}
1647c478bdstevel@tonic-gate
1657c478bdstevel@tonic-gate	*dbcp = dbc;
1667c478bdstevel@tonic-gate	return (0);
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gateerr:	__os_free(dbc, sizeof(*dbc));
1697c478bdstevel@tonic-gate	return (ret);
1707c478bdstevel@tonic-gate}
1717c478bdstevel@tonic-gate
1727c478bdstevel@tonic-gate/*
1737c478bdstevel@tonic-gate * __db_c_close --
1747c478bdstevel@tonic-gate *	Close the cursor (recycle for later use).
1757c478bdstevel@tonic-gate */
1767c478bdstevel@tonic-gatestatic int
1777c478bdstevel@tonic-gate__db_c_close(dbc)
1787c478bdstevel@tonic-gate	DBC *dbc;
1797c478bdstevel@tonic-gate{
1807c478bdstevel@tonic-gate	DB *dbp;
1817c478bdstevel@tonic-gate	int ret, t_ret;
1827c478bdstevel@tonic-gate
1837c478bdstevel@tonic-gate	dbp = dbc->dbp;
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate	DB_PANIC_CHECK(dbp);
1867c478bdstevel@tonic-gate
1877c478bdstevel@tonic-gate	ret = 0;
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate	/*
1907c478bdstevel@tonic-gate	 * We cannot release the lock until after we've called the
1917c478bdstevel@tonic-gate	 * access method specific routine, since btrees may have pending
1927c478bdstevel@tonic-gate	 * deletes.
1937c478bdstevel@tonic-gate	 */
1947c478bdstevel@tonic-gate
1957c478bdstevel@tonic-gate	/* Remove the cursor from the active queue. */
1967c478bdstevel@tonic-gate	DB_THREAD_LOCK(dbp);
1977c478bdstevel@tonic-gate	TAILQ_REMOVE(&dbp->active_queue, dbc, links);
1987c478bdstevel@tonic-gate	DB_THREAD_UNLOCK(dbp);
1997c478bdstevel@tonic-gate
2007c478bdstevel@tonic-gate	/* Call the access specific cursor close routine. */
2017c478bdstevel@tonic-gate	if ((t_ret = dbc->c_am_close(dbc)) != 0 && ret == 0)
2027c478bdstevel@tonic-gate		t_ret = ret;
2037c478bdstevel@tonic-gate
2047c478bdstevel@tonic-gate	/* Release the lock. */
2057c478bdstevel@tonic-gate	if (F_ISSET(dbc->dbp, DB_AM_CDB) && dbc->mylock != LOCK_INVALID) {
2067c478bdstevel@tonic-gate		ret = lock_put(dbc->dbp->dbenv->lk_info, dbc->mylock);
2077c478bdstevel@tonic-gate		dbc->mylock = LOCK_INVALID;
2087c478bdstevel@tonic-gate	}
2097c478bdstevel@tonic-gate
2107c478bdstevel@tonic-gate	/* Clean up the cursor. */
2117c478bdstevel@tonic-gate	dbc->flags = 0;
2127c478bdstevel@tonic-gate
2137c478bdstevel@tonic-gate#ifdef DEBUG
2147c478bdstevel@tonic-gate	/*
2157c478bdstevel@tonic-gate	 * Check for leftover locks, unless we're running with transactions.
2167c478bdstevel@tonic-gate	 *
2177c478bdstevel@tonic-gate	 * If we're running tests, display any locks currently held.  It's
2187c478bdstevel@tonic-gate	 * possible that some applications may hold locks for long periods,
2197c478bdstevel@tonic-gate	 * e.g., conference room locks, but the DB tests should never close
2207c478bdstevel@tonic-gate	 * holding locks.
2217c478bdstevel@tonic-gate	 */
2227c478bdstevel@tonic-gate	if (F_ISSET(dbp, DB_AM_LOCKING) && dbc->lid == dbc->locker) {
2237c478bdstevel@tonic-gate		DB_LOCKREQ request;
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gate		request.op = DB_LOCK_DUMP;
2267c478bdstevel@tonic-gate		if ((t_ret = lock_vec(dbp->dbenv->lk_info,
2277c478bdstevel@tonic-gate		    dbc->locker, 0, &request, 1, NULL)) != 0 && ret == 0)
2287c478bdstevel@tonic-gate			ret = EAGAIN;
2297c478bdstevel@tonic-gate	}
2307c478bdstevel@tonic-gate#endif
2317c478bdstevel@tonic-gate	/* Move the cursor to the free queue. */
2327c478bdstevel@tonic-gate	DB_THREAD_LOCK(dbp);
2337c478bdstevel@tonic-gate	TAILQ_INSERT_TAIL(&dbp->free_queue, dbc, links);
2347c478bdstevel@tonic-gate	DB_THREAD_UNLOCK(dbp);
2357c478bdstevel@tonic-gate
2367c478bdstevel@tonic-gate	return (ret);
2377c478bdstevel@tonic-gate}
2387c478bdstevel@tonic-gate
2397c478bdstevel@tonic-gate#ifdef DEBUG
2407c478bdstevel@tonic-gate/*
2417c478bdstevel@tonic-gate * __db_cprint --
2427c478bdstevel@tonic-gate *	Display the current cursor list.
2437c478bdstevel@tonic-gate *
2447c478bdstevel@tonic-gate * PUBLIC: int __db_cprint __P((DB *));
2457c478bdstevel@tonic-gate */
2467c478bdstevel@tonic-gateint
2477c478bdstevel@tonic-gate__db_cprint(dbp)
2487c478bdstevel@tonic-gate	DB *dbp;
2497c478bdstevel@tonic-gate{
2507c478bdstevel@tonic-gate	static const FN fn[] = {
2517c478bdstevel@tonic-gate		{ DBC_RECOVER, 	"recover" },
2527c478bdstevel@tonic-gate		{ DBC_RMW, 	"read-modify-write" },
2537c478bdstevel@tonic-gate		{ 0 },
2547c478bdstevel@tonic-gate	};
2557c478bdstevel@tonic-gate	DBC *dbc;
2567c478bdstevel@tonic-gate
2577c478bdstevel@tonic-gate	DB_THREAD_LOCK(dbp);
2587c478bdstevel@tonic-gate	for (dbc = TAILQ_FIRST(&dbp->active_queue);
2597c478bdstevel@tonic-gate	    dbc != NULL; dbc = TAILQ_NEXT(dbc, links)) {
2607c478bdstevel@tonic-gate		fprintf(stderr,
2617c478bdstevel@tonic-gate		    "%#0x: dbp: %#0x txn: %#0x lid: %lu locker: %lu",
2627c478bdstevel@tonic-gate		    (u_int)dbc, (u_int)dbc->dbp, (u_int)dbc->txn,
2637c478bdstevel@tonic-gate		    (u_long)dbc->lid, (u_long)dbc->locker);
2647c478bdstevel@tonic-gate		__db_prflags(dbc->flags, fn, stderr);
2657c478bdstevel@tonic-gate		fprintf(stderr, "\n");
2667c478bdstevel@tonic-gate	}
2677c478bdstevel@tonic-gate	DB_THREAD_UNLOCK(dbp);
2687c478bdstevel@tonic-gate
2697c478bdstevel@tonic-gate	return (0);
2707c478bdstevel@tonic-gate}
2717c478bdstevel@tonic-gate#endif /* DEBUG */
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gate/*
2747c478bdstevel@tonic-gate * __db_c_destroy --
2757c478bdstevel@tonic-gate *	Destroy the cursor.
2767c478bdstevel@tonic-gate *
2777c478bdstevel@tonic-gate * PUBLIC: int __db_c_destroy __P((DBC *));
2787c478bdstevel@tonic-gate */
2797c478bdstevel@tonic-gateint
2807c478bdstevel@tonic-gate__db_c_destroy(dbc)
2817c478bdstevel@tonic-gate	DBC *dbc;
2827c478bdstevel@tonic-gate{
2837c478bdstevel@tonic-gate	DB *dbp;
2847c478bdstevel@tonic-gate	int ret;
2857c478bdstevel@tonic-gate
2867c478bdstevel@tonic-gate	dbp = dbc->dbp;
2877c478bdstevel@tonic-gate
2887c478bdstevel@tonic-gate	/* Remove the cursor from the free queue. */
2897c478bdstevel@tonic-gate	DB_THREAD_LOCK(dbp);
2907c478bdstevel@tonic-gate	TAILQ_REMOVE(&dbp->free_queue, dbc, links);
2917c478bdstevel@tonic-gate	DB_THREAD_UNLOCK(dbp);
2927c478bdstevel@tonic-gate
2937c478bdstevel@tonic-gate	/* Call the access specific cursor destroy routine. */
2947c478bdstevel@tonic-gate	ret = dbc->c_am_destroy == NULL ? 0 : dbc->c_am_destroy(dbc);
2957c478bdstevel@tonic-gate
2967c478bdstevel@tonic-gate	/* Free up allocated memory. */
2977c478bdstevel@tonic-gate	if (dbc->rkey.data != NULL)
2987c478bdstevel@tonic-gate		__os_free(dbc->rkey.data, dbc->rkey.ulen);
2997c478bdstevel@tonic-gate	if (dbc->rdata.data != NULL)
3007c478bdstevel@tonic-gate		__os_free(dbc->rdata.data, dbc->rdata.ulen);
3017c478bdstevel@tonic-gate	__os_free(dbc, sizeof(*dbc));
3027c478bdstevel@tonic-gate
3037c478bdstevel@tonic-gate	return (0);
3047c478bdstevel@tonic-gate}
3057c478bdstevel@tonic-gate
3067c478bdstevel@tonic-gate/*
3077c478bdstevel@tonic-gate * db_fd --
3087c478bdstevel@tonic-gate *	Return a file descriptor for flock'ing.
3097c478bdstevel@tonic-gate */
3107c478bdstevel@tonic-gatestatic int
3117c478bdstevel@tonic-gate__db_fd(dbp, fdp)
3127c478bdstevel@tonic-gate        DB *dbp;
3137c478bdstevel@tonic-gate	int *fdp;
3147c478bdstevel@tonic-gate{
3157c478bdstevel@tonic-gate	DB_PANIC_CHECK(dbp);
3167c478bdstevel@tonic-gate
3177c478bdstevel@tonic-gate	/*
3187c478bdstevel@tonic-gate	 * XXX
3197c478bdstevel@tonic-gate	 * Truly spectacular layering violation.
3207c478bdstevel@tonic-gate	 */
3217c478bdstevel@tonic-gate	return (__mp_xxx_fd(dbp->mpf, fdp));
3227c478bdstevel@tonic-gate}
3237c478bdstevel@tonic-gate
3247c478bdstevel@tonic-gate/*
3257c478bdstevel@tonic-gate * __db_get --
3267c478bdstevel@tonic-gate *	Return a key/data pair.
3277c478bdstevel@tonic-gate */
3287c478bdstevel@tonic-gatestatic int
3297c478bdstevel@tonic-gate__db_get(dbp, txn, key, data, flags)
3307c478bdstevel@tonic-gate	DB *dbp;
3317c478bdstevel@tonic-gate	DB_TXN *txn;
3327c478bdstevel@tonic-gate	DBT *key, *data;
3337c478bdstevel@tonic-gate	u_int32_t flags;
3347c478bdstevel@tonic-gate{
3357c478bdstevel@tonic-gate	DBC *dbc;
3367c478bdstevel@tonic-gate	int ret, t_ret;
3377c478bdstevel@tonic-gate
3387c478bdstevel@tonic-gate	DB_PANIC_CHECK(dbp);
3397c478bdstevel@tonic-gate
3407c478bdstevel@tonic-gate	if ((ret = __db_getchk(dbp, key, data, flags)) != 0)
3417c478bdstevel@tonic-gate		return (ret);
3427c478bdstevel@tonic-gate
3437c478bdstevel@tonic-gate	if ((ret = dbp->cursor(dbp, txn, &dbc, 0)) != 0)
3447c478bdstevel@tonic-gate		return (ret);
3457c478bdstevel@tonic-gate
3467c478bdstevel@tonic-gate	DEBUG_LREAD(dbc, txn, "__db_get", key, NULL, flags);
3477c478bdstevel@tonic-gate
3487c478bdstevel@tonic-gate	ret = dbc->c_get(dbc, key, data,
3497c478bdstevel@tonic-gate	    flags == 0 || flags == DB_RMW ? flags | DB_SET : flags);
3507c478bdstevel@tonic-gate
3517c478bdstevel@tonic-gate	if ((t_ret = __db_c_close(dbc)) != 0 && ret == 0)
3527c478bdstevel@tonic-gate		ret = t_ret;
3537c478bdstevel@tonic-gate
3547c478bdstevel@tonic-gate	return (ret);
3557c478bdstevel@tonic-gate}
3567c478bdstevel@tonic-gate
3577c478bdstevel@tonic-gate/*
3587c478bdstevel@tonic-gate * __db_put --
3597c478bdstevel@tonic-gate *	Store a key/data pair.
3607c478bdstevel@tonic-gate */
3617c478bdstevel@tonic-gatestatic int
3627c478bdstevel@tonic-gate__db_put(dbp, txn, key, data, flags)
3637c478bdstevel@tonic-gate	DB *dbp;
3647c478bdstevel@tonic-gate	DB_TXN *txn;
3657c478bdstevel@tonic-gate	DBT *key, *data;
3667c478bdstevel@tonic-gate	u_int32_t flags;
3677c478bdstevel@tonic-gate{
3687c478bdstevel@tonic-gate	DBC *dbc;
3697c478bdstevel@tonic-gate	DBT tdata;
3707c478bdstevel@tonic-gate	int ret, t_ret;
3717c478bdstevel@tonic-gate
3727c478bdstevel@tonic-gate	DB_PANIC_CHECK(dbp);
3737c478bdstevel@tonic-gate
3747c478bdstevel@tonic-gate	if ((ret = __db_putchk(dbp, key, data,
3757c478bdstevel@tonic-gate	    flags, F_ISSET(dbp, DB_AM_RDONLY), F_ISSET(dbp, DB_AM_DUP))) != 0)
3767c478bdstevel@tonic-gate		return (ret);
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate	if ((ret = dbp->cursor(dbp, txn, &dbc, DB_WRITELOCK)) != 0)
3797c478bdstevel@tonic-gate		return (ret);
3807c478bdstevel@tonic-gate
3817c478bdstevel@tonic-gate	DEBUG_LWRITE(dbc, txn, "__db_put", key, data, flags);
3827c478bdstevel@tonic-gate
3837c478bdstevel@tonic-gate	if (flags == DB_NOOVERWRITE) {
3847c478bdstevel@tonic-gate		/*
3857c478bdstevel@tonic-gate		 * Set DB_DBT_USERMEM, this might be a threaded application and
3867c478bdstevel@tonic-gate		 * the flags checking will catch us.  We don't want the actual
3877c478bdstevel@tonic-gate		 * data, so request a partial of length 0.
3887c478bdstevel@tonic-gate		 */
3897c478bdstevel@tonic-gate		memset(&tdata, 0, sizeof(tdata));
3907c478bdstevel@tonic-gate		F_SET(&tdata, DB_DBT_USERMEM | DB_DBT_PARTIAL);
3917c478bdstevel@tonic-gate		if ((ret = dbc->c_get(dbc, key, &tdata, DB_SET | DB_RMW)) == 0)
3927c478bdstevel@tonic-gate			ret = DB_KEYEXIST;
3937c478bdstevel@tonic-gate		else if (ret == DB_NOTFOUND)
3947c478bdstevel@tonic-gate			ret = 0;
3957c478bdstevel@tonic-gate	}
3967c478bdstevel@tonic-gate	if (ret == 0)
3977c478bdstevel@tonic-gate		ret = dbc->c_put(dbc, key, data, DB_KEYLAST);
3987c478bdstevel@tonic-gate
3997c478bdstevel@tonic-gate	if ((t_ret = __db_c_close(dbc)) != 0 && ret == 0)
4007c478bdstevel@tonic-gate		ret = t_ret;
4017c478bdstevel@tonic-gate
4027c478bdstevel@tonic-gate	return (ret);
4037c478bdstevel@tonic-gate}
4047c478bdstevel@tonic-gate
4057c478bdstevel@tonic-gate/*
4067c478bdstevel@tonic-gate * __db_sync --
4077c478bdstevel@tonic-gate *	Flush the database cache.
4087c478bdstevel@tonic-gate *
4097c478bdstevel@tonic-gate * PUBLIC: int __db_sync __P((DB *, u_int32_t));
4107c478bdstevel@tonic-gate */
4117c478bdstevel@tonic-gateint
4127c478bdstevel@tonic-gate__db_sync(dbp, flags)
4137c478bdstevel@tonic-gate	DB *dbp;
4147c478bdstevel@tonic-gate	u_int32_t flags;
4157c478bdstevel@tonic-gate{
4167c478bdstevel@tonic-gate	int ret;
4177c478bdstevel@tonic-gate
4187c478bdstevel@tonic-gate	DB_PANIC_CHECK(dbp);
4197c478bdstevel@tonic-gate
4207c478bdstevel@tonic-gate	if ((ret = __db_syncchk(dbp, flags)) != 0)
4217c478bdstevel@tonic-gate		return (ret);
4227c478bdstevel@tonic-gate
4237c478bdstevel@tonic-gate	/* If it wasn't possible to modify the file, we're done. */
4247c478bdstevel@tonic-gate	if (F_ISSET(dbp, DB_AM_INMEM | DB_AM_RDONLY))
4257c478bdstevel@tonic-gate		return (0);
4267c478bdstevel@tonic-gate
4277c478bdstevel@tonic-gate	/* Flush any dirty pages from the cache to the backing file. */
4287c478bdstevel@tonic-gate	if ((ret = memp_fsync(dbp->mpf)) == DB_INCOMPLETE)
4297c478bdstevel@tonic-gate		ret = 0;
4307c478bdstevel@tonic-gate
4317c478bdstevel@tonic-gate	return (ret);
4327c478bdstevel@tonic-gate}
433