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>
|
|
|
|
|
2004-07-23 20:21:44 +00:00
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
#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>
|
|
|
|
|
|
|
|
#define __USE_GNU /* For O_DIRECT.. */
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
static int stable = -1;
|
2004-07-20 03:40:57 +00:00
|
|
|
/** Defined in bufferManager.c */
|
|
|
|
extern pthread_mutex_t add_pending_mutex;
|
2004-07-27 21:30:54 +00:00
|
|
|
static pthread_mutex_t stable_mutex;
|
|
|
|
|
|
|
|
static long myLseek(int f, long offset, int whence);
|
|
|
|
static long myLseekNoLock(int f, long offset, int whence);
|
|
|
|
|
2004-07-20 03:40:57 +00:00
|
|
|
|
2004-07-15 00:42:36 +00:00
|
|
|
void pageRead(Page *ret) {
|
2004-07-27 21:30:54 +00:00
|
|
|
/* long fileSize; */
|
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
|
|
|
/** @todo pageRead() is using fseek to calculate the file size on each read, which is inefficient. */
|
|
|
|
pageoffset = ret->id * PAGE_SIZE;
|
2004-07-27 21:30:54 +00:00
|
|
|
/* flockfile(stable); */
|
|
|
|
pthread_mutex_lock(&stable_mutex);
|
|
|
|
/* fileSize = myLseekNoLock(stable, 0, SEEK_END); */
|
2004-07-20 00:15:17 +00:00
|
|
|
|
|
|
|
/* DEBUG("Reading page %d\n", ret->id); */
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
/* if(!ret->memAddr) {
|
2004-07-15 00:42:36 +00:00
|
|
|
ret->memAddr = malloc(PAGE_SIZE);
|
|
|
|
}
|
2004-07-20 00:15:17 +00:00
|
|
|
if(!ret->memAddr) {
|
|
|
|
perror("pageFile.c");
|
|
|
|
fflush(NULL);
|
|
|
|
}
|
|
|
|
assert(ret->memAddr); */
|
2004-07-27 21:30:54 +00:00
|
|
|
|
|
|
|
/** @todo was manual extension of the storefile really necessary? */
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
/* if ((ret->id)*PAGE_SIZE >= fileSize) {
|
|
|
|
myLseekNoLock(stable, (ret->id - 1) * PAGE_SIZE -1, SEEK_SET);
|
2004-07-15 00:42:36 +00:00
|
|
|
if(1 != fwrite("", 1, 1, stable)) {
|
|
|
|
if(feof(stable)) { printf("Unexpected eof extending storefile!\n"); fflush(NULL); abort(); }
|
|
|
|
if(ferror(stable)) { printf("Error extending storefile! %d", ferror(stable)); fflush(NULL); abort(); }
|
2004-07-27 21:30:54 +00:00
|
|
|
}
|
|
|
|
}*/
|
2004-07-15 00:42:36 +00:00
|
|
|
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
offset = myLseekNoLock(stable, pageoffset, SEEK_SET);
|
|
|
|
assert(offset == pageoffset);
|
|
|
|
int read_size;
|
|
|
|
read_size = read(stable, ret->memAddr, PAGE_SIZE);
|
|
|
|
if(read_size != PAGE_SIZE) {
|
|
|
|
if (!read_size) {
|
|
|
|
long fileSize = myLseekNoLock(stable, 0, SEEK_END);
|
|
|
|
offset = myLseekNoLock(stable, pageoffset, SEEK_SET);
|
|
|
|
assert(offset == pageoffset);
|
|
|
|
if(fileSize <= pageoffset) {
|
|
|
|
memset(ret->memAddr, 0, PAGE_SIZE);
|
|
|
|
write(stable, ret->memAddr, PAGE_SIZE);
|
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
|
|
|
void pageWrite(Page * ret) {
|
|
|
|
|
|
|
|
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-07-27 21:30:54 +00:00
|
|
|
offset = myLseekNoLock(stable, pageoffset, SEEK_SET);
|
2004-07-20 00:15:17 +00:00
|
|
|
assert(offset == pageoffset);
|
|
|
|
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);
|
|
|
|
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 if(write_ret != PAGE_SIZE){
|
|
|
|
printf("write_ret is %d\n", write_ret);
|
|
|
|
fflush(NULL);
|
|
|
|
abort();
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
2004-07-27 21:30:54 +00:00
|
|
|
pthread_mutex_unlock(&stable_mutex);
|
2004-07-15 00:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void openPageFile() {
|
|
|
|
|
|
|
|
DEBUG("Opening storefile.\n");
|
2004-07-27 21:30:54 +00:00
|
|
|
/* if( ! (stable = fopen(STORE_FILE, "r+"))) { / * file may not exist * /
|
2004-07-15 00:42:36 +00:00
|
|
|
byte* zero = calloc(1, PAGE_SIZE);
|
|
|
|
|
|
|
|
if(!(stable = fopen(STORE_FILE, "w+"))) { perror("Couldn't open or create store file"); abort(); }
|
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
/ * Write out one page worth of zeros to get started. * /
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-07-27 21:30:54 +00:00
|
|
|
/ * if(1 != fwrite(zero, PAGE_SIZE, 1, stable)) { assert (0); } * /
|
2004-07-15 00:42:36 +00:00
|
|
|
|
|
|
|
free(zero);
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG("storefile opened.\n");
|
2004-07-27 21:30:54 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
stable = open (STORE_FILE, O_CREAT | O_RDWR | O_DIRECT, S_IRWXU | S_IRWXG | S_IRWXO);
|
|
|
|
|
|
|
|
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-07-27 21:30:54 +00:00
|
|
|
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-07-27 21:30:54 +00:00
|
|
|
long myLseekNoLock(int f, long offset, int whence) {
|
|
|
|
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-07-27 21:30:54 +00:00
|
|
|
long fileSize = myLseek(stable, 0, SEEK_END);
|
2004-07-15 00:42:36 +00:00
|
|
|
|
2004-07-26 22:01:09 +00:00
|
|
|
assert(! (fileSize % PAGE_SIZE));
|
|
|
|
return fileSize / PAGE_SIZE;
|
|
|
|
}
|