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>
|
2006-04-06 03:22:57 +00:00
|
|
|
#include <lladd/logger/logger2.h>
|
2006-04-14 03:45:26 +00:00
|
|
|
#include <lladd/truncation.h>
|
2004-07-27 21:30:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2006-10-05 00:46:18 +00:00
|
|
|
/** For O_DIRECT. It's unclear that this is the correct thing to \#define, but it works under linux. */
|
2004-08-17 01:46:17 +00:00
|
|
|
#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;
|
|
|
|
|
|
|
|
static long myLseekNoLock(int f, long offset, int whence);
|
|
|
|
|
2004-10-19 04:45:42 +00:00
|
|
|
static int oldOffset = -1;
|
|
|
|
|
2006-08-10 23:51:15 +00:00
|
|
|
int pageFile_isDurable = 1;
|
2004-10-19 04:45:42 +00:00
|
|
|
|
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... */
|
2006-09-27 20:27:49 +00:00
|
|
|
memset(ret->memAddr, 0, PAGE_SIZE); // The file will be extended when we write to the new page.
|
2004-07-27 21:30:54 +00:00
|
|
|
} else if(read_size == -1) {
|
|
|
|
perror("pageFile.c couldn't read");
|
|
|
|
fflush(NULL);
|
2006-06-13 22:40:53 +00:00
|
|
|
abort();
|
2004-07-27 21:30:54 +00:00
|
|
|
} 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. */
|
2006-06-12 20:05:28 +00:00
|
|
|
if(!dirtyPages_isDirty(ret)) {
|
2005-02-02 02:12:40 +00:00
|
|
|
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
|
|
|
|
2006-04-06 03:22:57 +00:00
|
|
|
// If necessary, force the log to disk so that ret's LSN will be stable.
|
|
|
|
LogForce(pageReadLSN(ret));
|
2004-07-15 00:42:36 +00:00
|
|
|
|
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
|
|
|
|
2006-04-14 03:45:26 +00:00
|
|
|
dirtyPages_remove(ret);
|
2005-02-02 02:12:40 +00:00
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_unlock(&stable_mutex);
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
2006-08-10 23:51:15 +00:00
|
|
|
//#define PAGE_FILE_O_DIRECT
|
|
|
|
|
2006-04-12 01:41:35 +00:00
|
|
|
/** @todo O_DIRECT is broken in older linuxes (eg 2.4). The build script should disable it on such platforms. */
|
2004-07-15 00:42:36 +00:00
|
|
|
void openPageFile() {
|
|
|
|
DEBUG("Opening storefile.\n");
|
2004-07-27 21:30:54 +00:00
|
|
|
|
2006-08-10 23:51:15 +00:00
|
|
|
#ifdef PAGE_FILE_O_DIRECT
|
2006-10-05 00:46:18 +00:00
|
|
|
stable = open (STORE_FILE, O_CREAT | O_RDWR | O_DIRECT, FILE_PERM); //S_IRWXU | S_IRWXG | S_IRWXO);
|
2006-08-10 23:51:15 +00:00
|
|
|
#else
|
2006-10-05 00:46:18 +00:00
|
|
|
stable = open (STORE_FILE, O_CREAT | O_RDWR, FILE_PERM);//S_IRWXU | S_IRWXG | S_IRWXO);
|
2006-08-10 23:51:15 +00:00
|
|
|
#endif
|
2006-10-05 00:46:18 +00:00
|
|
|
if(!pageFile_isDurable) {
|
2006-08-10 23:51:15 +00:00
|
|
|
fprintf(stderr, "\n**********\n");
|
|
|
|
fprintf (stderr, "pageFile.c: pageFile_isDurable==0; the page file will not force writes to disk.\n");
|
|
|
|
fprintf (stderr, " Transactions will not be durable if the system crashes.\n**********\n");
|
|
|
|
}
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-08-10 23:51:15 +00:00
|
|
|
void forcePageFile() {
|
2006-10-05 00:46:18 +00:00
|
|
|
if(pageFile_isDurable) {
|
2006-08-10 23:51:15 +00:00
|
|
|
#ifndef PAGE_FILE_O_DIRECT
|
|
|
|
#ifdef HAVE_FDATASYNC
|
|
|
|
fdatasync(stable);
|
|
|
|
#else
|
|
|
|
fsync(stable);
|
|
|
|
#endif // HAVE_FDATASYNC
|
|
|
|
#endif // PAGE_FILE_O_DIRECT
|
2006-10-05 00:46:18 +00:00
|
|
|
}
|
2006-08-10 23:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void closePageFile() {
|
|
|
|
forcePageFile();
|
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 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;
|
|
|
|
}
|
|
|
|
|