diff -u linux-2.5.15-flock/fs/locks.c linux-2.5.15-flock/fs/locks.c
--- linux-2.5.15-flock/fs/locks.c	Tue May 21 20:54:35 2002
+++ linux-2.5.15-flock/fs/locks.c	Tue May 21 21:45:51 2002
@@ -684,7 +684,7 @@
 	 */
 	spin_lock(&file_lock_lock);
 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
-		if (!(fl->fl_flags & FL_POSIX))
+		if (!IS_POSIX(fl))
 			continue;
 		if (fl->fl_owner != owner)
 			break;
@@ -697,7 +697,7 @@
 			 struct file *filp, loff_t offset,
 			 size_t count)
 {
-	struct file_lock *fl;
+	struct file_lock **before;
 	struct file_lock *new_fl = locks_alloc_lock(0);
 	int error;
 
@@ -719,31 +719,33 @@
 	/* Search the lock list for this inode for locks that conflict with
 	 * the proposed read/write.
 	 */
-	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
+	for_each_lock(inode, before) {
+		struct file_lock *fl = *before;
 		if (!IS_POSIX(fl))
 			continue;
 		if (fl->fl_start > new_fl->fl_end)
 			break;
-		if (posix_locks_conflict(new_fl, fl)) {
-			error = -EAGAIN;
-			if (filp && (filp->f_flags & O_NONBLOCK))
-				break;
-			error = -EDEADLK;
-			if (posix_locks_deadlock(new_fl, fl))
-				break;
-	
-			error = locks_block_on(fl, new_fl);
-			if (error != 0)
-				break;
+		if (!posix_locks_conflict(new_fl, fl))
+			continue;
+
+		error = -EAGAIN;
+		if (filp && (filp->f_flags & O_NONBLOCK))
+			break;
+		error = -EDEADLK;
+		if (posix_locks_deadlock(new_fl, fl))
+			break;
 	
-			/*
-			 * If we've been sleeping someone might have
-			 * changed the permissions behind our back.
-			 */
-			if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
-				break;
-			goto repeat;
-		}
+		error = locks_block_on(fl, new_fl);
+		if (error != 0)
+			break;
+
+		/*
+		 * If we've been sleeping someone might have
+		 * changed the permissions behind our back.
+		 */
+		if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
+			break;
+		goto repeat;
 	}
 	spin_unlock(&file_lock_lock);
 	locks_free_lock(new_fl);
@@ -992,7 +994,7 @@
 			if (error != 0)
 				goto out;
 			flock = inode->i_flock;
-			if (!(flock && (flock->fl_flags & FL_LEASE)))
+			if (!(flock && IS_LEASE(flock)))
 				goto out;
 		} while (flock->fl_type & F_INPROGRESS);
 	}
@@ -1017,7 +1019,7 @@
 	do {
 		fl->fl_type = future;
 		fl = fl->fl_next;
-	} while (fl != NULL && (fl->fl_flags & FL_LEASE));
+	} while (fl != NULL && IS_LEASE(fl));
 
 	kill_fasync(&flock->fl_fasync, SIGIO, POLL_MSG);
 
@@ -1038,7 +1040,7 @@
 		printk(KERN_WARNING "lease timed out\n");
 	} else if (error > 0) {
 		flock = inode->i_flock;
-		if (flock && (flock->fl_flags & FL_LEASE))
+		if (flock && IS_LEASE(flock))
 			goto restart;
 		error = 0;
 	}
@@ -1061,7 +1063,7 @@
 time_t lease_get_mtime(struct inode *inode)
 {
 	struct file_lock *flock = inode->i_flock;
-	if (flock && (flock->fl_flags & FL_LEASE) && (flock->fl_type & F_WRLCK))
+	if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK))
 		return CURRENT_TIME;
 	return inode->i_mtime;
 }
@@ -1084,7 +1086,7 @@
 	struct file_lock *fl;
 	
 	fl = filp->f_dentry->d_inode->i_flock;
-	if ((fl == NULL) || ((fl->fl_flags & FL_LEASE) == 0))
+	if ((fl == NULL) || !IS_LEASE(fl))
 		return F_UNLCK;
 	return fl->fl_type & ~F_INPROGRESS;
 }
@@ -1149,7 +1151,7 @@
 
 	for_each_lock(inode, before) {
 		struct file_lock *fl = *before;
-		if (fl->fl_flags != FL_LEASE)
+		if (!IS_LEASE(fl))
 			break;
 		if (fl->fl_file == filp)
 			my_before = before;
@@ -1349,8 +1351,7 @@
 	/* Don't allow mandatory locks on files that may be memory mapped
 	 * and shared.
 	 */
-	if (IS_MANDLOCK(inode) &&
-	    (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) {
+	if (MANDATORY_LOCK(inode)) {
 		struct address_space *mapping = inode->i_mapping;
 
 		if (!list_empty(&mapping->i_mmap_shared)) {
@@ -1392,12 +1393,12 @@
 		error = posix_lock_file(filp, file_lock);
 		if ((error != -EAGAIN) || (cmd == F_SETLK))
 			break;
-		error = wait_event_interruptible(lock->fl_wait, !lock->fl_next);
+		error = wait_event_interruptible(file_lock->fl_wait, !file_lock->fl_next);
 		if (!error)
 			continue;
 
 		spin_lock(&file_lock_lock);
-		locks_delete_block(lock);
+		locks_delete_block(file_lock);
 		spin_unlock(&file_lock_lock);
 		break;
 	}
@@ -1485,8 +1486,7 @@
 	/* Don't allow mandatory locks on files that may be memory mapped
 	 * and shared.
 	 */
-	if (IS_MANDLOCK(inode) &&
-	    (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) {
+	if (MANDATORY_LOCK(inode)) {
 		struct address_space *mapping = inode->i_mapping;
 
 		if (!list_empty(&mapping->i_mmap_shared)) {
@@ -1524,7 +1524,19 @@
 		if (error < 0)
 			goto out;
 	}
-	error = posix_lock_file(filp, file_lock);
+	for (;;) {
+		error = posix_lock_file(filp, file_lock);
+		if ((error != -EAGAIN) || (cmd == F_SETLK64))
+			break;
+		error = wait_event_interruptible(file_lock->fl_wait, !file_lock->fl_next);
+		if (!error)
+			continue;
+
+		spin_lock(&file_lock_lock);
+		locks_delete_block(file_lock);
+		spin_unlock(&file_lock_lock);
+		break;
+	}
 
 out:
 	if (error)
@@ -1615,20 +1627,18 @@
 		inode = fl->fl_file->f_dentry->d_inode;
 
 	out += sprintf(out, "%d:%s ", id, pfx);
-	if (fl->fl_flags & FL_POSIX) {
+	if (IS_POSIX(fl)) {
 		out += sprintf(out, "%6s %s ",
 			     (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
 			     (inode == NULL) ? "*NOINODE*" :
-			     (IS_MANDLOCK(inode) &&
-			      (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
-			     "MANDATORY" : "ADVISORY ");
-	} else if (fl->fl_flags & FL_FLOCK) {
+			     MANDATORY_LOCK(inode) ? "MANDATORY" : "ADVISORY ");
+	} else if (IS_FLOCK(fl)) {
 		if (fl->fl_type & LOCK_MAND) {
 			out += sprintf(out, "FLOCK  MSNFS     ");
 		} else {
 			out += sprintf(out, "FLOCK  ADVISORY  ");
 		}
-	} else if (fl->fl_flags & FL_LEASE) {
+	} else if (IS_LEASE(fl)) {
 		out += sprintf(out, "LEASE  MANDATORY ");
 	} else {
 		out += sprintf(out, "UNKNOWN UNKNOWN  ");
@@ -1740,12 +1750,12 @@
 	int result = 1;
 	spin_lock(&file_lock_lock);
 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
-		if (fl->fl_flags == FL_POSIX) {
+		if (IS_POSIX(fl)) {
 			if (fl->fl_type == F_RDLCK)
 				continue;
 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
 				continue;
-		} else if (fl->fl_flags == FL_FLOCK) {
+		} else if (IS_FLOCK(fl)) {
 			if (!(fl->fl_type & LOCK_MAND))
 				continue;
 			if (fl->fl_type & LOCK_READ)
@@ -1778,10 +1788,10 @@
 	int result = 1;
 	spin_lock(&file_lock_lock);
 	for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
-		if (fl->fl_flags == FL_POSIX) {
+		if (IS_POSIX(fl)) {
 			if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
 				continue;
-		} else if (fl->fl_flags == FL_FLOCK) {
+		} else if (IS_FLOCK(fl)) {
 			if (!(fl->fl_type & LOCK_MAND))
 				continue;
 			if (fl->fl_type & LOCK_WRITE)
