
Use mmap for the lock file, avoiding some pread/pwrite calls and making the
code more simple.



---

 cur-root/check.c  |    8 +++++++-
 cur-root/libjio.h |    1 +
 cur-root/trans.c  |   38 +++++++++++++++-----------------------
 3 files changed, 23 insertions(+), 24 deletions(-)

diff -puN trans.c~mmap_lock_file trans.c
--- cur/trans.c~mmap_lock_file	2004-07-13 18:16:44.794142896 -0300
+++ cur-root/trans.c	2004-07-13 18:16:44.799142136 -0300
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <dirent.h>
 #include <errno.h>
+#include <sys/mman.h>
 
 #include "libjio.h"
 #include "common.h"
@@ -29,18 +30,13 @@
 /* gets a new transaction id */
 static unsigned int get_tid(struct jfs *fs)
 {
-	unsigned int curid;
-	int r, rv;
+	unsigned int curid, rv;
 
 	/* lock the whole file */
 	plockf(fs->jfd, F_LOCKW, 0, 0);
 
 	/* read the current max. curid */
-	r = spread(fs->jfd, &curid, sizeof(curid), 0);
-	if (r != sizeof(curid)) {
-		rv = 0;
-		goto exit;
-	}
+	curid = *(fs->jmap);
 
 	/* increment it and handle overflows */
 	rv = curid + 1;
@@ -48,11 +44,7 @@ static unsigned int get_tid(struct jfs *
 		goto exit;
 
 	/* write to the file descriptor */
-	r = spwrite(fs->jfd, &rv, sizeof(rv), 0);
-	if (r != sizeof(curid)) {
-		rv = 0;
-		goto exit;
-	}
+	*(fs->jmap) = rv;
 
 exit:
 	plockf(fs->jfd, F_UNLOCK, 0, 0);
@@ -63,17 +55,13 @@ exit:
 static void free_tid(struct jfs *fs, unsigned int tid)
 {
 	unsigned int curid, i;
-	int r;
 	char name[PATH_MAX];
 
 	/* lock the whole file */
 	plockf(fs->jfd, F_LOCKW, 0, 0);
 
 	/* read the current max. curid */
-	r = spread(fs->jfd, &curid, sizeof(curid), 0);
-	if (r != sizeof(curid)) {
-		goto exit;
-	}
+	curid = *(fs->jmap);
 
 	if (tid < curid) {
 		/* we're not freeing the max. curid, so we just return */
@@ -92,10 +80,7 @@ static void free_tid(struct jfs *fs, uns
 		}
 
 		/* and save it */
-		r = spwrite(fs->jfd, &i, sizeof(i), 0);
-		if (r != sizeof(curid)) {
-			goto exit;
-		}
+		*(fs->jmap) = i;
 	}
 
 exit:
@@ -518,9 +503,9 @@ int jopen(struct jfs *fs, const char *na
 	 * jopen() simultaneously and both initialize the file */
 	plockf(jfd, F_LOCKW, 0, 0);
 	lstat(jlockfile, &sinfo);
-	if (sinfo.st_size == 0) {
+	if (sinfo.st_size != sizeof(unsigned int)) {
 		t = 1;
-		rv = write(jfd, &t, sizeof(t));
+		rv = spwrite(jfd, &t, sizeof(t), 0);
 		if (rv != sizeof(t)) {
 			plockf(jfd, F_UNLOCK, 0, 0);
 			return -1;
@@ -530,6 +515,11 @@ int jopen(struct jfs *fs, const char *na
 
 	fs->jfd = jfd;
 
+	fs->jmap = (int *) mmap(NULL, sizeof(unsigned int),
+			PROT_READ | PROT_WRITE, MAP_SHARED, jfd, 0);
+	if (fs->jmap == MAP_FAILED)
+		return -1;
+
 	return fd;
 }
 
@@ -565,6 +555,8 @@ int jclose(struct jfs *fs)
 	if (fs->name)
 		/* allocated by strdup() in jopen() */
 		free(fs->name);
+	munmap(fs->jmap, sizeof(unsigned int));
+
 	return 0;
 }
 
diff -puN libjio.h~mmap_lock_file libjio.h
--- cur/libjio.h~mmap_lock_file	2004-07-13 18:16:44.795142744 -0300
+++ cur-root/libjio.h	2004-07-13 18:16:44.799142136 -0300
@@ -26,6 +26,7 @@ struct jfs {
 	char *name;		/* and its name */
 	int jdirfd;		/* journal directory file descriptor */
 	int jfd;		/* journal's lock file descriptor */
+	int *jmap;		/* journal's lock file mmap area */
 	int flags;		/* journal flags */
 	struct jlinger *ltrans;	/* lingered transactions */
 	pthread_mutex_t lock;	/* a soft lock used in some operations */
diff -puN check.c~mmap_lock_file check.c
--- cur/check.c~mmap_lock_file	2004-07-13 18:16:44.797142440 -0300
+++ cur-root/check.c	2004-07-13 18:16:44.799142136 -0300
@@ -93,7 +93,8 @@ error:
 /* check the journal and rollback incomplete transactions */
 int jfsck(const char *name, struct jfsck_result *res)
 {
-	int fd, tfd, rv, i, maxtid;
+	int fd, tfd, rv, i;
+	unsigned int maxtid;
 	char jdir[PATH_MAX], jlockfile[PATH_MAX], tname[PATH_MAX];
 	struct stat sinfo;
 	struct jfs fs;
@@ -129,6 +130,11 @@ int jfsck(const char *name, struct jfsck
 		return J_ENOJOURNAL;
 	fs.jfd = rv;
 
+	fs.jmap = (int *) mmap(NULL, sizeof(unsigned int),
+			PROT_READ | PROT_WRITE, MAP_SHARED, fs.jfd, 0);
+	if (fs.jmap == MAP_FAILED)
+		return J_ENOJOURNAL;
+
 	dir = opendir(jdir);
 	if (dir == NULL)
 		return J_ENOJOURNAL;

_
