2004-07-15 00:42:36 +00:00
|
|
|
/**
|
2004-08-17 01:46:17 +00:00
|
|
|
@file
|
2004-07-15 00:42:36 +00:00
|
|
|
This file handles all of the file I/O for pages.
|
|
|
|
|
|
|
|
*/
|
2004-07-23 20:21:44 +00:00
|
|
|
#include "page.h"
|
2004-07-15 00:42:36 +00:00
|
|
|
#include <lladd/bufferManager.h>
|
|
|
|
|
|
|
|
#include "pageFile.h"
|
|
|
|
#include <assert.h>
|
|
|
|
#include "logger/logWriter.h"
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2004-08-17 01:46:17 +00:00
|
|
|
/** For O_DIRECT. It's unclear that this is the correct thing to #define, but it works under linux. */
|
|
|
|
#define __USE_GNU
|
2004-07-27 21:30:54 +00:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2004-08-21 00:03:30 +00:00
|
|
|
/** Allows boostrapping of the header page. */
|
|
|
|
#include <lladd/operations/pageOperations.h>
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
static int stable = -1;
|
|
|
|
static pthread_mutex_t stable_mutex;
|
|
|
|
|
2004-10-06 06:08:09 +00:00
|
|
|
/* static long myLseek(int f, long offset, int whence); */
|
2004-07-27 21:30:54 +00:00
|
|
|
static long myLseekNoLock(int f, long offset, int whence);
|
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
static int oldOffset = -1;
|
|
|
|
|
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
void pageRead(Page *ret) {
|
2004-07-20 00:15:17 +00:00
|
|
|
|
|
|
|
long pageoffset;
|
2004-07-15 00:42:36 +00:00
|
|
|
long offset;
|
|
|
|
|
2004-07-20 00:15:17 +00:00
|
|
|
pageoffset = ret->id * PAGE_SIZE;
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_lock(&stable_mutex);
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
|
|
|
|
if(oldOffset != pageoffset) {
|
|
|
|
offset = myLseekNoLock(stable, pageoffset, SEEK_SET);
|
|
|
|
assert(offset == pageoffset);
|
|
|
|
} else {
|
|
|
|
offset = oldOffset;
|
|
|
|
}
|
|
|
|
oldOffset = offset + PAGE_SIZE;
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
assert(offset == pageoffset);
|
|
|
|
int read_size;
|
|
|
|
read_size = read(stable, ret->memAddr, PAGE_SIZE);
|
|
|
|
if(read_size != PAGE_SIZE) {
|
2004-10-19 04:45:42 +00:00
|
|
|
if (!read_size) { /* Past EOF... */
|
|
|
|
/* long fileSize = myLseekNoLock(stable, 0, SEEK_END);
|
2004-07-27 21:30:54 +00:00
|
|
|
offset = myLseekNoLock(stable, pageoffset, SEEK_SET);
|
2004-10-19 04:45:42 +00:00
|
|
|
assert(offset == pageoffset); */
|
|
|
|
/* if(fileSize <= pageoffset) { */
|
2004-08-17 01:46:17 +00:00
|
|
|
memset(ret->memAddr, 0, PAGE_SIZE);
|
2004-10-19 04:45:42 +00:00
|
|
|
/* write(stable, ret->memAddr, PAGE_SIZE); */ /* all this does is extend the file..why would we bother doing that? :)
|
|
|
|
} */
|
2004-07-27 21:30:54 +00:00
|
|
|
} else if(read_size == -1) {
|
|
|
|
perror("pageFile.c couldn't read");
|
|
|
|
fflush(NULL);
|
|
|
|
assert(0);
|
|
|
|
} else {
|
|
|
|
printf("pageFile.c readfile: read_size = %d, errno = %d\n", read_size, errno);
|
|
|
|
abort();
|
|
|
|
}
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_unlock(&stable_mutex);
|
2004-07-20 00:15:17 +00:00
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
2005-02-02 02:12:40 +00:00
|
|
|
/** @todo need to sync the page file to disk occasionally, so that the
|
|
|
|
dirty page table can be kept up to date. */
|
2004-07-15 00:42:36 +00:00
|
|
|
void pageWrite(Page * ret) {
|
2005-02-02 02:12:40 +00:00
|
|
|
/** If the page is clean, there's no reason to write it out. */
|
|
|
|
if(!ret->dirty) {
|
|
|
|
DEBUG(" =^)~ ");
|
|
|
|
return;
|
|
|
|
}
|
2004-07-15 00:42:36 +00:00
|
|
|
long pageoffset = ret->id * PAGE_SIZE;
|
2004-07-20 00:15:17 +00:00
|
|
|
long offset ;
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-07-21 02:13:28 +00:00
|
|
|
/* assert(ret->pending == 0); */
|
2004-07-20 03:40:57 +00:00
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
if(flushedLSN() < pageReadLSN(ret)) {
|
2004-07-20 00:15:17 +00:00
|
|
|
DEBUG("pageWrite is calling syncLog()!\n");
|
2004-07-15 00:42:36 +00:00
|
|
|
syncLog();
|
|
|
|
}
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_lock(&stable_mutex);
|
2004-07-27 22:04:59 +00:00
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
if(oldOffset != pageoffset) {
|
|
|
|
offset = myLseekNoLock(stable, pageoffset, SEEK_SET);
|
|
|
|
assert(offset == pageoffset);
|
|
|
|
} else {
|
|
|
|
offset = oldOffset;
|
|
|
|
}
|
|
|
|
oldOffset = offset + PAGE_SIZE;
|
2004-07-20 00:15:17 +00:00
|
|
|
assert(ret->memAddr);
|
|
|
|
|
|
|
|
/* DEBUG("Writing page %d\n", ret->id); */
|
2004-07-27 21:30:54 +00:00
|
|
|
int write_ret = write(stable, ret->memAddr, PAGE_SIZE);
|
2004-10-19 04:45:42 +00:00
|
|
|
if(write_ret != PAGE_SIZE) {
|
|
|
|
if(-1 == write_ret) {
|
|
|
|
perror("pageFile.c couldn't write");
|
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
|
|
|
} else if(0 == write_ret) {
|
|
|
|
/* now what? */
|
|
|
|
printf("write_ret is zero\n");
|
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
|
|
|
} else {
|
|
|
|
printf("write_ret is %d\n", write_ret);
|
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
|
|
|
}
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
2005-02-02 02:12:40 +00:00
|
|
|
|
|
|
|
ret->dirty = 0;
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_unlock(&stable_mutex);
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
2005-01-24 19:58:09 +00:00
|
|
|
/** @todo O_DIRECT is broken on old (pre 2.6.2ish?) linux, so it's disabled until the build script can be improved. :( */
|
2004-07-15 00:42:36 +00:00
|
|
|
void openPageFile() {
|
|
|
|
|
|
|
|
DEBUG("Opening storefile.\n");
|
2004-07-27 21:30:54 +00:00
|
|
|
|
2005-03-02 05:47:38 +00:00
|
|
|
#if HAVE_POSIX_MEMALIGN
|
|
|
|
// O_DIRECT is broken under linux 2.4..
|
2005-01-24 19:58:09 +00:00
|
|
|
stable = open (STORE_FILE, O_CREAT | O_RDWR /*| O_DIRECT*/, S_IRWXU | S_IRWXG | S_IRWXO);
|
2005-03-02 05:47:38 +00:00
|
|
|
#else
|
2005-03-08 07:53:53 +00:00
|
|
|
#warn Not using O_DIRECT
|
2005-03-02 05:47:38 +00:00
|
|
|
// If we don't have posix_memalign(), then we aren't aligning our pages in memory, and can't use O_DIRECT.
|
|
|
|
stable = open (STORE_FILE, O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
#endif
|
2004-07-27 21:30:54 +00:00
|
|
|
if(stable == -1) {
|
|
|
|
perror("couldn't open storefile");
|
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_init(&stable_mutex, NULL);
|
2004-07-15 00:42:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
void closePageFile() {
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
int ret = close(stable);
|
|
|
|
|
|
|
|
if(-1 == ret) {
|
|
|
|
perror("Couldn't close storefile.");
|
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
stable = -1;
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
static long myLseek(int f, long offset, int whence) {
|
2004-07-15 00:42:36 +00:00
|
|
|
long ret;
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_lock(&stable_mutex);
|
|
|
|
ret = myLseekNoLock(f, offset, whence);
|
|
|
|
pthread_mutex_unlock(&stable_mutex);
|
2004-07-20 00:15:17 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
static long myLseekNoLock(int f, long offset, int whence) {
|
2004-07-27 21:30:54 +00:00
|
|
|
assert(! ( offset % 4096 ));
|
|
|
|
long ret = lseek(f, offset, whence);
|
|
|
|
if(ret == -1) {
|
|
|
|
perror("Couldn't seek.");
|
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
|
|
|
}
|
2004-07-15 00:42:36 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
/*void myFwrite(const void * dat, long size, FILE * f) {
|
|
|
|
int nmemb = fwrite(dat, size, 1, f);
|
|
|
|
/ * test * /
|
2004-07-15 00:42:36 +00:00
|
|
|
if(nmemb != 1) {
|
|
|
|
perror("myFwrite");
|
|
|
|
abort();
|
2004-07-27 21:30:54 +00:00
|
|
|
/ * return FILE_WRITE_OPEN_ERROR; * /
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
}*/
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-07-26 22:01:09 +00:00
|
|
|
long pageCount() {
|
2004-10-19 04:45:42 +00:00
|
|
|
pthread_mutex_lock(&stable_mutex);
|
|
|
|
printf(".");
|
2004-07-27 21:30:54 +00:00
|
|
|
long fileSize = myLseek(stable, 0, SEEK_END);
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
oldOffset = -1;
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&stable_mutex);
|
2004-07-26 22:01:09 +00:00
|
|
|
assert(! (fileSize % PAGE_SIZE));
|
|
|
|
return fileSize / PAGE_SIZE;
|
|
|
|
}
|