git » libjio » commit 60f270d

According to my fcntl's manpage, POSIX 1003.1-2001 allows l_len parameter to be negative, and inside jread() and jreadv() we took advantage of that. However, it also says that it's supported in Linux only since kernel 2.5.49 and 2.4.21, which is quite new.

author Alberto Bertogli
2004-05-04 23:56:59 UTC
committer Alberto Bertogli
2007-07-15 12:44:26 UTC
parent 338b0e7c7f306de861c2f39744782f5fbc78eaee

According to my fcntl's manpage, POSIX 1003.1-2001 allows l_len parameter to be negative, and inside jread() and jreadv() we took advantage of that. However, it also says that it's supported in Linux only since kernel 2.5.49 and 2.4.21, which is quite new.

According to my fcntl's manpage, POSIX 1003.1-2001 allows l_len parameter to
be negative, and inside jread() and jreadv() we took advantage of that.
However, it also says that it's supported in Linux only since kernel 2.5.49
and 2.4.21, which is quite new.

With this patch we remove the need to use negative lenghts in lockf(), and use
our plockf() wrapper instead.

This has the bonus that on some systems the relation between lockf and fcntl
is unspecified, so we now use fcntl for all our locks.

Thanks to Pieter Grimmerink.

libjio.c +8 -4

diff --git a/libjio.c b/libjio.c
index c6b446c..f634a01 100644
--- a/libjio.c
+++ b/libjio.c
@@ -503,11 +503,13 @@ int jopen(struct jfs *fs, const char *name, int flags, int mode, int jflags)
 ssize_t jread(struct jfs *fs, void *buf, size_t count)
 {
 	int rv;
+	off_t pos;
 
 	pthread_mutex_lock(&(fs->lock));
-	lockf(fs->fd, F_LOCK, count);
+	pos = lseek(fs->fd, 0, SEEK_CUR);
+	plockf(fs->fd, F_LOCK, pos, count);
 	rv = read(fs->fd, buf, count);
-	lockf(fs->fd, F_ULOCK, -count);
+	plockf(fs->fd, F_ULOCK, pos, count);
 	pthread_mutex_unlock(&(fs->lock));
 
 	return rv;
@@ -530,15 +532,17 @@ ssize_t jreadv(struct jfs *fs, struct iovec *vector, int count)
 {
 	int rv, i;
 	size_t sum;
+	off_t pos;
 	
 	sum = 0;
 	for (i = 0; i < count; i++)
 		sum += vector[i].iov_len;
 	
 	pthread_mutex_lock(&(fs->lock));
-	lockf(fs->fd, F_LOCK, sum);
+	pos = lseek(fs->fd, 0, SEEK_CUR);
+	plockf(fs->fd, F_LOCK, pos, count);
 	rv = readv(fs->fd, vector, count);
-	lockf(fs->fd, F_ULOCK, -sum);
+	plockf(fs->fd, F_ULOCK, pos, count);
 	pthread_mutex_unlock(&(fs->lock));
 
 	return rv;