patch-2.0.37 linux/fs/locks.c

Next file: linux/fs/ncpfs/Config.in
Previous file: linux/fs/isofs/inode.c
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.0.36/linux/fs/locks.c linux/fs/locks.c
@@ -107,6 +107,7 @@
 #include <linux/kernel.h>
 #include <linux/errno.h>
 #include <linux/stat.h>
+#include <linux/file.h>
 #include <linux/fcntl.h>
 
 #include <asm/segment.h>
@@ -255,17 +256,25 @@
 {
 	struct file_lock file_lock;
 	struct file *filp;
-
-	if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
-		return (-EBADF);
+	int err = -EINVAL;
+	
+	filp = fget(fd);
+	if(filp==NULL)
+		return -EBADF;
+	
 
 	if (!flock_make_lock(filp, &file_lock, cmd))
-		return (-EINVAL);
-	
-	if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
-		return (-EBADF);
+		goto out;
 
-	return (flock_lock_file(filp, &file_lock, (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1));
+	if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
+	{
+		err = -EBADF;
+		goto out;
+	}
+	err=flock_lock_file(filp, &file_lock, (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
+out:
+	fput(filp, filp->f_inode);
+	return err;	
 }
 
 /* Report the first existing lock that would conflict with l.
@@ -278,19 +287,27 @@
 	struct file *filp;
 	struct file_lock *fl,file_lock;
 
-	if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
-		return (-EBADF);
 	error = verify_area(VERIFY_WRITE, l, sizeof(*l));
 	if (error)
 		return (error);
 
+	filp = fget(fd);
+	if(filp==NULL)
+		return -EBADF;
+
 	memcpy_fromfs(&flock, l, sizeof(flock));
 	if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
-		return (-EINVAL);
+	{
+		error = -EINVAL;
+		goto out;
+	}
 
 	if (!filp->f_inode || !posix_make_lock(filp, &file_lock, &flock))
-		return (-EINVAL);
-
+	{
+		error = -EINVAL;
+		goto out;
+	}
+	
 	flock.l_type = F_UNLCK;
 	for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
 		if (!(fl->fl_flags & FL_POSIX))
@@ -307,7 +324,9 @@
 	}
 
 	memcpy_tofs(l, &flock, sizeof(flock));
-	return (0);
+out:
+	fput(filp, filp->f_inode);
+	return error;
 }
 
 /* Apply the lock described by l to an open file descriptor.
@@ -326,19 +345,20 @@
 	/* Get arguments and validate them ...
 	 */
 
-	if ((fd >= NR_OPEN) || !(filp = current->files->fd[fd]))
-		return (-EBADF);
-	
 	error = verify_area(VERIFY_READ, l, sizeof(*l));
 	if (error)
 		return (error);
-	
-	if (!(inode = filp->f_inode))
-		return (-EINVAL);
-	
+		
+	filp = fget(fd);
+	if(filp==NULL)
+		return -EBADF;
+
+	inode = filp->f_inode;
+		
 	/*
 	 * This might block, so we do it before checking the inode.
 	 */
+
 	memcpy_fromfs(&flock, l, sizeof(flock));
 
 	/* Don't allow mandatory locks on files that may be memory mapped
@@ -350,22 +370,34 @@
 		struct vm_area_struct *vma = inode->i_mmap;
 		do {
 			if (vma->vm_flags & VM_MAYSHARE)
-				return (-EAGAIN);
+			{
+				error = -EAGAIN;
+				goto out;
+			}
 			vma = vma->vm_next_share;
 		} while (vma != inode->i_mmap);
 	}
 
 	if (!posix_make_lock(filp, &file_lock, &flock))
-		return (-EINVAL);
+	{
+		error = -EINVAL;
+		goto out;
+	}
 	
 	switch (flock.l_type) {
 	case F_RDLCK:
 		if (!(filp->f_mode & 1))
-			return (-EBADF);
+		{
+			error = -EBADF;
+			goto out;
+		}
 		break;
 	case F_WRLCK:
 		if (!(filp->f_mode & 2))
-			return (-EBADF);
+		{
+			error = -EBADF;
+			goto out;
+		}
 		break;
 	case F_UNLCK:
 		break;
@@ -384,13 +416,19 @@
 }
 #endif
 		if (!(filp->f_mode & 3))
-			return (-EBADF);
+		{
+			error = -EBADF;
+			goto out;
+		}
 		break;
 	default:
 		return (-EINVAL);
 	}
 	
-	return (posix_lock_file(filp, &file_lock, cmd == F_SETLKW));
+	error = posix_lock_file(filp, &file_lock, cmd == F_SETLKW);
+out:
+	fput(filp, filp->f_inode);
+	return error;
 }
 
 /* This function is called when the file is closed.

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen, slshen@lbl.gov