/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996, 1997, 1998
* Sleepycat Software. All rights reserved.
*/
#include "config.h"
#ifndef lint
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <errno.h>
#include <string.h>
#include <unistd.h>
#endif
#include "db_int.h"
#include "shqueue.h"
#include "db_shash.h"
#include "mp.h"
#include "common_ext.h"
/*
* __memp_bhwrite --
* Write the page associated with a given bucket header.
*
* PUBLIC: int __memp_bhwrite
* PUBLIC: __P((DB_MPOOL *, MPOOLFILE *, BH *, int *, int *));
*/
int
{
*restartp = 0;
*wrotep = 0;
incremented = 0;
/*
* Walk the process' DB_MPOOLFILE list and find a file descriptor for
* the file. We also check that the descriptor is open for writing.
* If we find a descriptor on the file that's not open for writing, we
* try and upgrade it to make it writeable. If that fails, we're done.
*/
return (0);
}
/*
* Increment the reference count -- see the comment in
* memp_fclose().
*/
incremented = 1;
break;
}
goto found;
/*
* It's not a page from a file we've opened. If the file requires
* information as to how to write this type of file. If not, there's
* nothing we can do.
*/
break;
return (0);
}
/*
* Try and open the file, attaching to the underlying shared area.
*
* XXX
* Don't try to attach to temporary files. There are two problems in
* trying to do that. First, if we have different privileges than the
* process that "owns" the temporary file, we might create the backing
* buffers, e.g., memp_trickle() running as root creating a file owned
* as root, mode 600. Second, if the temporary file has already been
* created, we don't have any way of finding out what its real name is,
* and, even if we did, it was already unlinked (so that it won't be
* left if the process dies horribly). This decision causes a problem,
* however: if the temporary file consumes the entire buffer cache,
* and the owner doesn't flush the buffers to disk, we could end up
* with resource starvation, and the memp_trickle() thread couldn't do
* anything about it. That's a pretty unlikely scenario, though.
*
* XXX
* There's no negative cache, so we may repeatedly try and open files
* that we have previously tried (and failed) to open.
*
* Ignore any error, assume it's a permissions problem.
*/
return (0);
return (0);
if (incremented) {
}
return (ret);
}
/*
* __memp_pgread --
* Read a page from a file.
*
* PUBLIC: int __memp_pgread __P((DB_MPOOLFILE *, BH *, int));
*/
int
int can_create;
{
/*
* Temporary files may not yet have been created. We don't create
* them now, we create them when the pages have to be flushed.
*/
nr = 0;
ret = 0;
else {
/*
* Ignore read errors if we have permission to create the page.
* Assume that the page doesn't exist, and that we'll create it
* when we write it out.
*/
}
created = 0;
if (can_create)
created = 1;
else {
/* If we had a short read, ret may be 0. */
if (ret == 0)
"%s: page %lu doesn't exist, create flag not set",
goto err;
}
/*
* Clear any bytes we didn't read that need to be cleared. If we're
* running in diagnostic mode, smash any bytes on the page that are
* unknown quantities for the caller.
*/
#ifdef DIAGNOSTIC
#endif
}
/* Call any pgin function. */
/* Unlock the buffer and reacquire the region lock. */
/*
* If no errors occurred, the data is now valid, clear the BH_TRASH
* flag; regardless, clear the lock bit and let other threads proceed.
*/
if (ret == 0) {
/* Update the statistics. */
if (created) {
} else {
}
}
return (ret);
}
/*
* __memp_pgwrite --
* Write a page to a file.
*
* PUBLIC: int __memp_pgwrite __P((DB_MPOOLFILE *, BH *, int *, int *));
*/
int
{
const char *fail;
*restartp = 0;
*wrotep = 0;
callpgin = 0;
/*
* Check the dirty bit -- this buffer may have been written since we
* decided to write it.
*/
*wrotep = 1;
return (0);
}
/*
* If there were two writers, we may have just been waiting while the
* other writer completed I/O on this buffer. Check the dirty bit one
* more time.
*/
*wrotep = 1;
return (0);
}
*restartp = 1;
/* Copy the LSN off the page if we're going to need it. */
/* Ensure the appropriate log records are on disk. */
goto err;
/*
* Call any pgout function. We set the callpgin flag so that we flag
* that the contents of the buffer will need to be passed through pgin
* before they are reused.
*/
ret = 0;
else {
callpgin = 1;
goto err;
}
/* Temporary files may not yet have been created. */
"unable to create temporary backing file");
goto err;
}
}
/* Write the page. */
fail = "write";
goto syserr;
}
fail = "write";
goto syserr;
}
*wrotep = 1;
/* Unlock the buffer and reacquire the region lock. */
/*
* Clean up the flags based on a successful write.
*
* If we rewrote the page, it will need processing by the pgin
* routine before reuse.
*/
if (callpgin)
/*
* If we write a buffer for which a checkpoint is waiting, update
* the count of pending buffers (both in the mpool as a whole and
* for this file). If the count for this file goes to zero, set a
* flag so we flush the writes.
*/
} else
dosync = 0;
/* Update I/O statistics. */
/*
* Do the sync after everything else has been updated, so any incoming
* checkpoint doesn't see inconsistent information.
*
* XXX:
* Don't lock the region around the sync, fsync(2) has no atomicity
* issues.
*
* XXX:
* We ignore errors from the sync -- it makes no sense to return an
* error to the calling process, so set a flag causing the checkpoint
* to be retried later. There is a possibility, of course, that a
* subsequent checkpoint was started and that we're going to force it
* to fail. That should be unlikely, and fixing it would be difficult.
*/
if (dosync) {
if (syncfail)
}
return (0);
err: /* Unlock the buffer and reacquire the region lock. */
/*
* Clean up the flags based on a failure.
*
* The page remains dirty but we remove our lock. If we rewrote the
* page, it will need processing by the pgin routine before reuse.
*/
if (callpgin)
return (ret);
}
/*
* __memp_pg --
*
* PUBLIC: int __memp_pg __P((DB_MPOOLFILE *, BH *, int));
*/
int
int is_pgin;
{
continue;
if (mfp->pgcookie_len == 0)
else {
}
if (is_pgin) {
goto err;
} else
goto err;
break;
}
return (0);
return (ret);
}
/*
* __memp_bhfree --
* Free a bucket header and its referenced data.
*
* PUBLIC: void __memp_bhfree __P((DB_MPOOL *, MPOOLFILE *, BH *, int));
*/
void
int free_mem;
{
/* Delete the buffer header from the hash bucket queue. */
/* Delete the buffer header from the LRU queue. */
/*
* If we're not reusing it immediately, free the buffer header
* and data for real.
*/
if (free_mem) {
}
}
/*
* __memp_upgrade --
* Upgrade a file descriptor from readonly to readwrite.
*/
static int
{
char *rpath;
/*
* !!!
* We expect the handle to already be locked.
*/
/* Check to see if we've already upgraded. */
return (0);
/* Check to see if we've already failed. */
return (1);
/*
* We know we have a valid pathname for the file because it's the only
* way we could have gotten a file descriptor of any kind.
*/
return (ret);
ret = 1;
} else {
/* Swap the descriptors and set the upgrade flag. */
ret = 0;
}
return (ret);
}