Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[pandora-kernel.git] / fs / locks.c
index 4fa269b..735b8d3 100644 (file)
@@ -326,17 +326,18 @@ static inline int flock_translate_cmd(int cmd) {
 }
 
 /* Fill in a file_lock structure with an appropriate FLOCK lock. */
-static int flock_make_lock(struct file *filp, struct file_lock **lock,
-               unsigned int cmd)
+static struct file_lock *
+flock_make_lock(struct file *filp, unsigned int cmd)
 {
        struct file_lock *fl;
        int type = flock_translate_cmd(cmd);
+
        if (type < 0)
-               return type;
+               return ERR_PTR(type);
        
        fl = locks_alloc_lock();
        if (fl == NULL)
-               return -ENOMEM;
+               return ERR_PTR(-ENOMEM);
 
        fl->fl_file = filp;
        fl->fl_owner = filp;
@@ -345,8 +346,7 @@ static int flock_make_lock(struct file *filp, struct file_lock **lock,
        fl->fl_type = type;
        fl->fl_end = OFFSET_MAX;
        
-       *lock = fl;
-       return 0;
+       return fl;
 }
 
 static int assign_type(struct file_lock *fl, long type)
@@ -427,14 +427,34 @@ static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
 }
 
 /* default lease lock manager operations */
-static void lease_break_callback(struct file_lock *fl)
+static bool
+lease_break_callback(struct file_lock *fl)
 {
        kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
+       return false;
+}
+
+static void
+lease_setup(struct file_lock *fl, void **priv)
+{
+       struct file *filp = fl->fl_file;
+       struct fasync_struct *fa = *priv;
+
+       /*
+        * fasync_insert_entry() returns the old entry if any. If there was no
+        * old entry, then it used "priv" and inserted it into the fasync list.
+        * Clear the pointer to indicate that it shouldn't be freed.
+        */
+       if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
+               *priv = NULL;
+
+       __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
 }
 
 static const struct lock_manager_operations lease_manager_ops = {
        .lm_break = lease_break_callback,
        .lm_change = lease_modify,
+       .lm_setup = lease_setup,
 };
 
 /*
@@ -445,7 +465,7 @@ static int lease_init(struct file *filp, long type, struct file_lock *fl)
        if (assign_type(fl, type) != 0)
                return -EINVAL;
 
-       fl->fl_owner = current->files;
+       fl->fl_owner = filp;
        fl->fl_pid = current->tgid;
 
        fl->fl_file = filp;
@@ -1274,7 +1294,7 @@ static void lease_clear_pending(struct file_lock *fl, int arg)
 }
 
 /* We already had a lease on this file; just change its type */
-int lease_modify(struct file_lock **before, int arg)
+int lease_modify(struct file_lock **before, int arg, struct list_head *dispose)
 {
        struct file_lock *fl = *before;
        int error = assign_type(fl, arg);
@@ -1293,7 +1313,7 @@ int lease_modify(struct file_lock **before, int arg)
                        printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
                        fl->fl_fasync = NULL;
                }
-               locks_delete_lock(before, NULL);
+               locks_delete_lock(before, dispose);
        }
        return 0;
 }
@@ -1307,18 +1327,20 @@ static bool past_time(unsigned long then)
        return time_after(jiffies, then);
 }
 
-static void time_out_leases(struct inode *inode)
+static void time_out_leases(struct inode *inode, struct list_head *dispose)
 {
        struct file_lock **before;
        struct file_lock *fl;
 
+       lockdep_assert_held(&inode->i_lock);
+
        before = &inode->i_flock;
        while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
                trace_time_out_leases(inode, fl);
                if (past_time(fl->fl_downgrade_time))
-                       lease_modify(before, F_RDLCK);
+                       lease_modify(before, F_RDLCK, dispose);
                if (past_time(fl->fl_break_time))
-                       lease_modify(before, F_UNLCK);
+                       lease_modify(before, F_UNLCK, dispose);
                if (fl == *before)      /* lease_modify may have freed fl */
                        before = &fl->fl_next;
        }
@@ -1331,6 +1353,20 @@ static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
        return locks_conflict(breaker, lease);
 }
 
+static bool
+any_leases_conflict(struct inode *inode, struct file_lock *breaker)
+{
+       struct file_lock *fl;
+
+       lockdep_assert_held(&inode->i_lock);
+
+       for (fl = inode->i_flock ; fl && IS_LEASE(fl); fl = fl->fl_next) {
+               if (leases_conflict(fl, breaker))
+                       return true;
+       }
+       return false;
+}
+
 /**
  *     __break_lease   -       revoke all outstanding leases on file
  *     @inode: the inode of the file to return
@@ -1347,12 +1383,11 @@ static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
 int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
 {
        int error = 0;
-       struct file_lock *new_fl, *flock;
-       struct file_lock *fl;
+       struct file_lock *new_fl;
+       struct file_lock *fl, **before;
        unsigned long break_time;
-       int i_have_this_lease = 0;
-       bool lease_conflict = false;
        int want_write = (mode & O_ACCMODE) != O_RDONLY;
+       LIST_HEAD(dispose);
 
        new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
        if (IS_ERR(new_fl))
@@ -1361,20 +1396,9 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
 
        spin_lock(&inode->i_lock);
 
-       time_out_leases(inode);
-
-       flock = inode->i_flock;
-       if ((flock == NULL) || !IS_LEASE(flock))
-               goto out;
+       time_out_leases(inode, &dispose);
 
-       for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
-               if (leases_conflict(fl, new_fl)) {
-                       lease_conflict = true;
-                       if (fl->fl_owner == current->files)
-                               i_have_this_lease = 1;
-               }
-       }
-       if (!lease_conflict)
+       if (!any_leases_conflict(inode, new_fl))
                goto out;
 
        break_time = 0;
@@ -1384,7 +1408,9 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
                        break_time++;   /* so that 0 means no break time */
        }
 
-       for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
+       for (before = &inode->i_flock;
+                       ((fl = *before) != NULL) && IS_LEASE(fl);
+                       before = &fl->fl_next) {
                if (!leases_conflict(fl, new_fl))
                        continue;
                if (want_write) {
@@ -1393,51 +1419,56 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
                        fl->fl_flags |= FL_UNLOCK_PENDING;
                        fl->fl_break_time = break_time;
                } else {
-                       if (lease_breaking(flock))
+                       if (lease_breaking(inode->i_flock))
                                continue;
                        fl->fl_flags |= FL_DOWNGRADE_PENDING;
                        fl->fl_downgrade_time = break_time;
                }
-               fl->fl_lmops->lm_break(fl);
+               if (fl->fl_lmops->lm_break(fl))
+                       locks_delete_lock(before, &dispose);
        }
 
-       if (i_have_this_lease || (mode & O_NONBLOCK)) {
+       fl = inode->i_flock;
+       if (!fl || !IS_LEASE(fl))
+               goto out;
+
+       if (mode & O_NONBLOCK) {
                trace_break_lease_noblock(inode, new_fl);
                error = -EWOULDBLOCK;
                goto out;
        }
 
 restart:
-       break_time = flock->fl_break_time;
+       break_time = inode->i_flock->fl_break_time;
        if (break_time != 0)
                break_time -= jiffies;
        if (break_time == 0)
                break_time++;
-       locks_insert_block(flock, new_fl);
+       locks_insert_block(inode->i_flock, new_fl);
        trace_break_lease_block(inode, new_fl);
        spin_unlock(&inode->i_lock);
+       locks_dispose_list(&dispose);
        error = wait_event_interruptible_timeout(new_fl->fl_wait,
                                                !new_fl->fl_next, break_time);
        spin_lock(&inode->i_lock);
        trace_break_lease_unblock(inode, new_fl);
        locks_delete_block(new_fl);
        if (error >= 0) {
-               if (error == 0)
-                       time_out_leases(inode);
                /*
                 * Wait for the next conflicting lease that has not been
                 * broken yet
                 */
-               for (flock = inode->i_flock; flock && IS_LEASE(flock);
-                               flock = flock->fl_next) {
-                       if (leases_conflict(new_fl, flock))
-                               goto restart;
-               }
+               if (error == 0)
+                       time_out_leases(inode, &dispose);
+               if (any_leases_conflict(inode, new_fl))
+                       goto restart;
+
                error = 0;
        }
 
 out:
        spin_unlock(&inode->i_lock);
+       locks_dispose_list(&dispose);
        locks_free_lock(new_fl);
        return error;
 }
@@ -1502,9 +1533,10 @@ int fcntl_getlease(struct file *filp)
        struct file_lock *fl;
        struct inode *inode = file_inode(filp);
        int type = F_UNLCK;
+       LIST_HEAD(dispose);
 
        spin_lock(&inode->i_lock);
-       time_out_leases(file_inode(filp));
+       time_out_leases(file_inode(filp), &dispose);
        for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
                        fl = fl->fl_next) {
                if (fl->fl_file == filp) {
@@ -1513,6 +1545,7 @@ int fcntl_getlease(struct file *filp)
                }
        }
        spin_unlock(&inode->i_lock);
+       locks_dispose_list(&dispose);
        return type;
 }
 
@@ -1550,6 +1583,7 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
        struct inode *inode = dentry->d_inode;
        bool is_deleg = (*flp)->fl_flags & FL_DELEG;
        int error;
+       LIST_HEAD(dispose);
 
        lease = *flp;
        trace_generic_add_lease(inode, lease);
@@ -1572,6 +1606,8 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
                return -EINVAL;
        }
 
+       spin_lock(&inode->i_lock);
+       time_out_leases(inode, &dispose);
        error = check_conflicting_open(dentry, arg);
        if (error)
                goto out;
@@ -1607,10 +1643,11 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
        }
 
        if (my_before != NULL) {
-               error = lease->fl_lmops->lm_change(my_before, arg);
-               if (!error)
-                       *flp = *my_before;
-               goto out;
+               lease = *my_before;
+               error = lease->fl_lmops->lm_change(my_before, arg, &dispose);
+               if (error)
+                       goto out;
+               goto out_setup;
        }
 
        error = -EINVAL;
@@ -1631,9 +1668,17 @@ generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **pr
        error = check_conflicting_open(dentry, arg);
        if (error)
                goto out_unlink;
+
+out_setup:
+       if (lease->fl_lmops->lm_setup)
+               lease->fl_lmops->lm_setup(lease, priv);
 out:
+       spin_unlock(&inode->i_lock);
+       locks_dispose_list(&dispose);
        if (is_deleg)
                mutex_unlock(&inode->i_mutex);
+       if (!error && !my_before)
+               *flp = NULL;
        return error;
 out_unlink:
        locks_unlink_lock(before);
@@ -1646,7 +1691,10 @@ static int generic_delete_lease(struct file *filp)
        struct file_lock *fl, **before;
        struct dentry *dentry = filp->f_path.dentry;
        struct inode *inode = dentry->d_inode;
+       LIST_HEAD(dispose);
 
+       spin_lock(&inode->i_lock);
+       time_out_leases(inode, &dispose);
        for (before = &inode->i_flock;
                        ((fl = *before) != NULL) && IS_LEASE(fl);
                        before = &fl->fl_next) {
@@ -1655,21 +1703,22 @@ static int generic_delete_lease(struct file *filp)
        }
        trace_generic_delete_lease(inode, fl);
        if (fl)
-               error = fl->fl_lmops->lm_change(before, F_UNLCK);
+               error = fl->fl_lmops->lm_change(before, F_UNLCK, &dispose);
+       spin_unlock(&inode->i_lock);
+       locks_dispose_list(&dispose);
        return error;
 }
 
 /**
  *     generic_setlease        -       sets a lease on an open file
- *     @filp: file pointer
- *     @arg: type of lease to obtain
- *     @flp: input - file_lock to use, output - file_lock inserted
- *     @priv: private data for lm_setup
+ *     @filp:  file pointer
+ *     @arg:   type of lease to obtain
+ *     @flp:   input - file_lock to use, output - file_lock inserted
+ *     @priv:  private data for lm_setup (may be NULL if lm_setup
+ *             doesn't require it)
  *
  *     The (input) flp->fl_lmops->lm_break function is required
  *     by break_lease().
- *
- *     Called with inode->i_lock held.
  */
 int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
                        void **priv)
@@ -1686,8 +1735,6 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
        if (error)
                return error;
 
-       time_out_leases(inode);
-
        switch (arg) {
        case F_UNLCK:
                return generic_delete_lease(filp);
@@ -1704,47 +1751,36 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
 }
 EXPORT_SYMBOL(generic_setlease);
 
-static int
-__vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
-{
-       if (filp->f_op->setlease)
-               return filp->f_op->setlease(filp, arg, lease, priv);
-       else
-               return generic_setlease(filp, arg, lease, priv);
-}
-
 /**
  * vfs_setlease        -       sets a lease on an open file
- * @filp: file pointer
- * @arg: type of lease to obtain
- * @lease: file_lock to use when adding a lease
- * @priv: private info for lm_setup when adding a lease
+ * @filp:      file pointer
+ * @arg:       type of lease to obtain
+ * @lease:     file_lock to use when adding a lease
+ * @priv:      private info for lm_setup when adding a lease (may be
+ *             NULL if lm_setup doesn't require it)
  *
  * Call this to establish a lease on the file. The "lease" argument is not
  * used for F_UNLCK requests and may be NULL. For commands that set or alter
  * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set;
  * if not, this function will return -ENOLCK (and generate a scary-looking
  * stack trace).
+ *
+ * The "priv" pointer is passed directly to the lm_setup function as-is. It
+ * may be NULL if the lm_setup operation doesn't require it.
  */
-
 int
 vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
 {
-       struct inode *inode = file_inode(filp);
-       int error;
-
-       spin_lock(&inode->i_lock);
-       error = __vfs_setlease(filp, arg, lease, priv);
-       spin_unlock(&inode->i_lock);
-
-       return error;
+       if (filp->f_op->setlease)
+               return filp->f_op->setlease(filp, arg, lease, priv);
+       else
+               return generic_setlease(filp, arg, lease, priv);
 }
 EXPORT_SYMBOL_GPL(vfs_setlease);
 
 static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
 {
-       struct file_lock *fl, *ret;
-       struct inode *inode = file_inode(filp);
+       struct file_lock *fl;
        struct fasync_struct *new;
        int error;
 
@@ -1757,26 +1793,9 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
                locks_free_lock(fl);
                return -ENOMEM;
        }
-       ret = fl;
-       spin_lock(&inode->i_lock);
-       error = __vfs_setlease(filp, arg, &ret, NULL);
-       if (error)
-               goto out_unlock;
-       if (ret == fl)
-               fl = NULL;
-
-       /*
-        * fasync_insert_entry() returns the old entry if any.
-        * If there was no old entry, then it used 'new' and
-        * inserted it into the fasync list. Clear new so that
-        * we don't release it here.
-        */
-       if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
-               new = NULL;
+       new->fa_fd = fd;
 
-       __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
-out_unlock:
-       spin_unlock(&inode->i_lock);
+       error = vfs_setlease(filp, arg, &fl, (void **)&new);
        if (fl)
                locks_free_lock(fl);
        if (new)
@@ -1866,9 +1885,12 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
            !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
                goto out_putf;
 
-       error = flock_make_lock(f.file, &lock, cmd);
-       if (error)
+       lock = flock_make_lock(f.file, cmd);
+       if (IS_ERR(lock)) {
+               error = PTR_ERR(lock);
                goto out_putf;
+       }
+
        if (can_sleep)
                lock->fl_flags |= FL_SLEEP;
 
@@ -2371,7 +2393,7 @@ void locks_remove_file(struct file *filp)
        while ((fl = *before) != NULL) {
                if (fl->fl_file == filp) {
                        if (IS_LEASE(fl)) {
-                               lease_modify(before, F_UNLCK);
+                               lease_modify(before, F_UNLCK, &dispose);
                                continue;
                        }