]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - fs/locks.c
locks: fix leaks on setlease errors
[net-next-2.6.git] / fs / locks.c
index 50ec15927aab3164a456875e6c11b1f4fa2f8bef..63fbc41cc573c0161c19ed83a1f16819acd0f7fd 100644 (file)
@@ -1371,20 +1371,22 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
        struct inode *inode = dentry->d_inode;
        int error, rdlease_count = 0, wrlease_count = 0;
 
+       lease = *flp;
+
+       error = -EACCES;
        if ((current_fsuid() != inode->i_uid) && !capable(CAP_LEASE))
-               return -EACCES;
+               goto out;
+       error = -EINVAL;
        if (!S_ISREG(inode->i_mode))
-               return -EINVAL;
+               goto out;
        error = security_file_lock(filp, arg);
        if (error)
-               return error;
+               goto out;
 
        time_out_leases(inode);
 
        BUG_ON(!(*flp)->fl_lmops->fl_break);
 
-       lease = *flp;
-
        if (arg != F_UNLCK) {
                error = -EAGAIN;
                if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
@@ -1441,7 +1443,8 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
        return 0;
 
 out:
-       locks_free_lock(lease);
+       if (arg != F_UNLCK)
+               locks_free_lock(lease);
        return error;
 }
 EXPORT_SYMBOL(generic_setlease);
@@ -1493,17 +1496,16 @@ int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
 }
 EXPORT_SYMBOL_GPL(vfs_setlease);
 
-/**
- *     fcntl_setlease  -       sets a lease on an open file
- *     @fd: open file descriptor
- *     @filp: file pointer
- *     @arg: type of lease to obtain
- *
- *     Call this fcntl to establish a lease on the file.
- *     Note that you also need to call %F_SETSIG to
- *     receive a signal when the lease is broken.
- */
-int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
+static int do_fcntl_delete_lease(struct file *filp)
+{
+       struct file_lock fl, *flp = &fl;
+
+       lease_init(filp, F_UNLCK, flp);
+
+       return vfs_setlease(filp, F_UNLCK, &flp);
+}
+
+static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
 {
        struct file_lock *fl;
        struct fasync_struct *new;
@@ -1521,7 +1523,7 @@ int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
        }
        lock_flocks();
        error = __vfs_setlease(filp, arg, &fl);
-       if (error || arg == F_UNLCK)
+       if (error)
                goto out_unlock;
 
        /*
@@ -1549,6 +1551,23 @@ out_unlock:
        return error;
 }
 
+/**
+ *     fcntl_setlease  -       sets a lease on an open file
+ *     @fd: open file descriptor
+ *     @filp: file pointer
+ *     @arg: type of lease to obtain
+ *
+ *     Call this fcntl to establish a lease on the file.
+ *     Note that you also need to call %F_SETSIG to
+ *     receive a signal when the lease is broken.
+ */
+int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
+{
+       if (arg == F_UNLCK)
+               return do_fcntl_delete_lease(filp);
+       return do_fcntl_add_lease(fd, filp, arg);
+}
+
 /**
  * flock_lock_file_wait - Apply a FLOCK-style lock to a file
  * @filp: The file to apply the lock to