locks: clean up vfs_setlease kerneldoc comments
[linux-2.6-block.git] / fs / locks.c
index a6f54802d277304b2f483eb61af3ae6b574a79f9..e16c2c61a44f3fa5eb856535674b25981476c9d0 100644 (file)
@@ -230,8 +230,12 @@ void locks_release_private(struct file_lock *fl)
                        fl->fl_ops->fl_release_private(fl);
                fl->fl_ops = NULL;
        }
-       fl->fl_lmops = NULL;
 
+       if (fl->fl_lmops) {
+               if (fl->fl_lmops->lm_put_owner)
+                       fl->fl_lmops->lm_put_owner(fl);
+               fl->fl_lmops = NULL;
+       }
 }
 EXPORT_SYMBOL_GPL(locks_release_private);
 
@@ -247,6 +251,18 @@ void locks_free_lock(struct file_lock *fl)
 }
 EXPORT_SYMBOL(locks_free_lock);
 
+static void
+locks_dispose_list(struct list_head *dispose)
+{
+       struct file_lock *fl;
+
+       while (!list_empty(dispose)) {
+               fl = list_first_entry(dispose, struct file_lock, fl_block);
+               list_del_init(&fl->fl_block);
+               locks_free_lock(fl);
+       }
+}
+
 void locks_init_lock(struct file_lock *fl)
 {
        memset(fl, 0, sizeof(struct file_lock));
@@ -255,21 +271,10 @@ void locks_init_lock(struct file_lock *fl)
 
 EXPORT_SYMBOL(locks_init_lock);
 
-static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
-{
-       if (fl->fl_ops) {
-               if (fl->fl_ops->fl_copy_lock)
-                       fl->fl_ops->fl_copy_lock(new, fl);
-               new->fl_ops = fl->fl_ops;
-       }
-       if (fl->fl_lmops)
-               new->fl_lmops = fl->fl_lmops;
-}
-
 /*
  * Initialize a new lock from an existing file_lock structure.
  */
-void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
+void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
 {
        new->fl_owner = fl->fl_owner;
        new->fl_pid = fl->fl_pid;
@@ -278,21 +283,30 @@ void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
        new->fl_type = fl->fl_type;
        new->fl_start = fl->fl_start;
        new->fl_end = fl->fl_end;
+       new->fl_lmops = fl->fl_lmops;
        new->fl_ops = NULL;
-       new->fl_lmops = NULL;
+
+       if (fl->fl_lmops) {
+               if (fl->fl_lmops->lm_get_owner)
+                       fl->fl_lmops->lm_get_owner(new, fl);
+       }
 }
-EXPORT_SYMBOL(__locks_copy_lock);
+EXPORT_SYMBOL(locks_copy_conflock);
 
 void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
 {
-       locks_release_private(new);
+       /* "new" must be a freshly-initialized lock */
+       WARN_ON_ONCE(new->fl_ops);
+
+       locks_copy_conflock(new, fl);
 
-       __locks_copy_lock(new, fl);
        new->fl_file = fl->fl_file;
        new->fl_ops = fl->fl_ops;
-       new->fl_lmops = fl->fl_lmops;
 
-       locks_copy_private(new, fl);
+       if (fl->fl_ops) {
+               if (fl->fl_ops->fl_copy_lock)
+                       fl->fl_ops->fl_copy_lock(new, fl);
+       }
 }
 
 EXPORT_SYMBOL(locks_copy_lock);
@@ -650,12 +664,16 @@ static void locks_unlink_lock(struct file_lock **thisfl_p)
  *
  * Must be called with i_lock held!
  */
-static void locks_delete_lock(struct file_lock **thisfl_p)
+static void locks_delete_lock(struct file_lock **thisfl_p,
+                             struct list_head *dispose)
 {
        struct file_lock *fl = *thisfl_p;
 
        locks_unlink_lock(thisfl_p);
-       locks_free_lock(fl);
+       if (dispose)
+               list_add(&fl->fl_block, dispose);
+       else
+               locks_free_lock(fl);
 }
 
 /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
@@ -718,7 +736,7 @@ posix_test_lock(struct file *filp, struct file_lock *fl)
                        break;
        }
        if (cfl) {
-               __locks_copy_lock(fl, cfl);
+               locks_copy_conflock(fl, cfl);
                if (cfl->fl_nspid)
                        fl->fl_pid = pid_vnr(cfl->fl_nspid);
        } else
@@ -811,6 +829,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
        struct inode * inode = file_inode(filp);
        int error = 0;
        int found = 0;
+       LIST_HEAD(dispose);
 
        if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
                new_fl = locks_alloc_lock();
@@ -833,7 +852,7 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
                if (request->fl_type == fl->fl_type)
                        goto out;
                found = 1;
-               locks_delete_lock(before);
+               locks_delete_lock(before, &dispose);
                break;
        }
 
@@ -880,6 +899,7 @@ out:
        spin_unlock(&inode->i_lock);
        if (new_fl)
                locks_free_lock(new_fl);
+       locks_dispose_list(&dispose);
        return error;
 }
 
@@ -893,6 +913,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
        struct file_lock **before;
        int error;
        bool added = false;
+       LIST_HEAD(dispose);
 
        /*
         * We may need two file_lock structures for this operation,
@@ -921,7 +942,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
                        if (!posix_locks_conflict(request, fl))
                                continue;
                        if (conflock)
-                               __locks_copy_lock(conflock, fl);
+                               locks_copy_conflock(conflock, fl);
                        error = -EAGAIN;
                        if (!(request->fl_flags & FL_SLEEP))
                                goto out;
@@ -988,7 +1009,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
                        else
                                request->fl_end = fl->fl_end;
                        if (added) {
-                               locks_delete_lock(before);
+                               locks_delete_lock(before, &dispose);
                                continue;
                        }
                        request = fl;
@@ -1018,21 +1039,24 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
                                 * one (This may happen several times).
                                 */
                                if (added) {
-                                       locks_delete_lock(before);
+                                       locks_delete_lock(before, &dispose);
                                        continue;
                                }
-                               /* Replace the old lock with the new one.
-                                * Wake up anybody waiting for the old one,
-                                * as the change in lock type might satisfy
-                                * their needs.
+                               /*
+                                * Replace the old lock with new_fl, and
+                                * remove the old one. It's safe to do the
+                                * insert here since we know that we won't be
+                                * using new_fl later, and that the lock is
+                                * just replacing an existing lock.
                                 */
-                               locks_wake_up_blocks(fl);
-                               fl->fl_start = request->fl_start;
-                               fl->fl_end = request->fl_end;
-                               fl->fl_type = request->fl_type;
-                               locks_release_private(fl);
-                               locks_copy_private(fl, request);
-                               request = fl;
+                               error = -ENOLCK;
+                               if (!new_fl)
+                                       goto out;
+                               locks_copy_lock(new_fl, request);
+                               request = new_fl;
+                               new_fl = NULL;
+                               locks_delete_lock(before, &dispose);
+                               locks_insert_lock(before, request);
                                added = true;
                        }
                }
@@ -1093,6 +1117,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
                locks_free_lock(new_fl);
        if (new_fl2)
                locks_free_lock(new_fl2);
+       locks_dispose_list(&dispose);
        return error;
 }
 
@@ -1268,7 +1293,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);
+               locks_delete_lock(before, NULL);
        }
        return 0;
 }
@@ -1431,8 +1456,18 @@ EXPORT_SYMBOL(__break_lease);
  */
 void lease_get_mtime(struct inode *inode, struct timespec *time)
 {
-       struct file_lock *flock = inode->i_flock;
-       if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
+       bool has_lease = false;
+       struct file_lock *flock;
+
+       if (inode->i_flock) {
+               spin_lock(&inode->i_lock);
+               flock = inode->i_flock;
+               if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
+                       has_lease = true;
+               spin_unlock(&inode->i_lock);
+       }
+
+       if (has_lease)
                *time = current_fs_time(inode->i_sb);
        else
                *time = inode->i_mtime;
@@ -1595,29 +1630,30 @@ static int generic_add_lease(struct file *filp, long arg, struct file_lock **flp
        smp_mb();
        error = check_conflicting_open(dentry, arg);
        if (error)
-               locks_unlink_lock(flp);
+               locks_unlink_lock(before);
 out:
        if (is_deleg)
                mutex_unlock(&inode->i_mutex);
        return error;
 }
 
-static int generic_delete_lease(struct file *filp, struct file_lock **flp)
+static int generic_delete_lease(struct file *filp)
 {
+       int error = -EAGAIN;
        struct file_lock *fl, **before;
        struct dentry *dentry = filp->f_path.dentry;
        struct inode *inode = dentry->d_inode;
 
-       trace_generic_delete_lease(inode, *flp);
-
        for (before = &inode->i_flock;
                        ((fl = *before) != NULL) && IS_LEASE(fl);
                        before = &fl->fl_next) {
-               if (fl->fl_file != filp)
-                       continue;
-               return (*flp)->fl_lmops->lm_change(before, F_UNLCK);
+               if (fl->fl_file == filp)
+                       break;
        }
-       return -EAGAIN;
+       trace_generic_delete_lease(inode, fl);
+       if (fl)
+               error = fl->fl_lmops->lm_change(before, F_UNLCK);
+       return error;
 }
 
 /**
@@ -1647,13 +1683,15 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
 
        time_out_leases(inode);
 
-       BUG_ON(!(*flp)->fl_lmops->lm_break);
-
        switch (arg) {
        case F_UNLCK:
-               return generic_delete_lease(filp, flp);
+               return generic_delete_lease(filp);
        case F_RDLCK:
        case F_WRLCK:
+               if (!(*flp)->fl_lmops->lm_break) {
+                       WARN_ON_ONCE(1);
+                       return -ENOLCK;
+               }
                return generic_add_lease(filp, arg, flp);
        default:
                return -EINVAL;
@@ -1670,30 +1708,16 @@ static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
 }
 
 /**
- *     vfs_setlease        -       sets a lease on an open file
- *     @filp: file pointer
- *     @arg: type of lease to obtain
- *     @lease: file_lock to use
- *
- *     Call this to establish a lease on the file.
- *     The (*lease)->fl_lmops->lm_break operation must be set; if not,
- *     break_lease will oops!
- *
- *     This will call the filesystem's setlease file method, if
- *     defined.  Note that there is no getlease method; instead, the
- *     filesystem setlease method should call back to setlease() to
- *     add a lease to the inode's lease list, where fcntl_getlease() can
- *     find it.  Since fcntl_getlease() only reports whether the current
- *     task holds a lease, a cluster filesystem need only do this for
- *     leases held by processes on this node.
- *
- *     There is also no break_lease method; filesystems that
- *     handle their own leases should break leases themselves from the
- *     filesystem's open, create, and (on truncate) setattr methods.
- *
- *     Warning: the only current setlease methods exist only to disable
- *     leases in certain cases.  More vfs changes may be required to
- *     allow a full filesystem lease implementation.
+ * 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
+ *
+ * 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).
  */
 
 int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
@@ -1709,15 +1733,6 @@ int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
 }
 EXPORT_SYMBOL_GPL(vfs_setlease);
 
-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, *ret;
@@ -1737,13 +1752,10 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
        ret = fl;
        spin_lock(&inode->i_lock);
        error = __vfs_setlease(filp, arg, &ret);
-       if (error) {
-               spin_unlock(&inode->i_lock);
-               locks_free_lock(fl);
-               goto out_free_fasync;
-       }
-       if (ret != fl)
-               locks_free_lock(fl);
+       if (error)
+               goto out_unlock;
+       if (ret == fl)
+               fl = NULL;
 
        /*
         * fasync_insert_entry() returns the old entry if any.
@@ -1754,10 +1766,11 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
        if (!fasync_insert_entry(fd, filp, &ret->fl_fasync, new))
                new = NULL;
 
-       error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
+       __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
+out_unlock:
        spin_unlock(&inode->i_lock);
-
-out_free_fasync:
+       if (fl)
+               locks_free_lock(fl);
        if (new)
                fasync_free(new);
        return error;
@@ -1776,7 +1789,7 @@ out_free_fasync:
 int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
 {
        if (arg == F_UNLCK)
-               return do_fcntl_delete_lease(filp);
+               return vfs_setlease(filp, F_UNLCK, NULL);
        return do_fcntl_add_lease(fd, filp, arg);
 }
 
@@ -1959,11 +1972,13 @@ int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
        if (file_lock.fl_type != F_UNLCK) {
                error = posix_lock_to_flock(&flock, &file_lock);
                if (error)
-                       goto out;
+                       goto rel_priv;
        }
        error = -EFAULT;
        if (!copy_to_user(l, &flock, sizeof(flock)))
                error = 0;
+rel_priv:
+       locks_release_private(&file_lock);
 out:
        return error;
 }
@@ -2184,7 +2199,8 @@ int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
        error = -EFAULT;
        if (!copy_to_user(l, &flock, sizeof(flock)))
                error = 0;
-  
+
+       locks_release_private(&file_lock);
 out:
        return error;
 }
@@ -2320,6 +2336,7 @@ void locks_remove_file(struct file *filp)
        struct inode * inode = file_inode(filp);
        struct file_lock *fl;
        struct file_lock **before;
+       LIST_HEAD(dispose);
 
        if (!inode->i_flock)
                return;
@@ -2365,12 +2382,13 @@ void locks_remove_file(struct file *filp)
                                fl->fl_type, fl->fl_flags,
                                fl->fl_start, fl->fl_end);
 
-                       locks_delete_lock(before);
+                       locks_delete_lock(before, &dispose);
                        continue;
                }
                before = &fl->fl_next;
        }
        spin_unlock(&inode->i_lock);
+       locks_dispose_list(&dispose);
 }
 
 /**
@@ -2452,7 +2470,11 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl,
                        seq_puts(f, "FLOCK  ADVISORY  ");
                }
        } else if (IS_LEASE(fl)) {
-               seq_puts(f, "LEASE  ");
+               if (fl->fl_flags & FL_DELEG)
+                       seq_puts(f, "DELEG  ");
+               else
+                       seq_puts(f, "LEASE  ");
+
                if (lease_breaking(fl))
                        seq_puts(f, "BREAKING  ");
                else if (fl->fl_file)
@@ -2565,86 +2587,6 @@ static int __init proc_locks_init(void)
 module_init(proc_locks_init);
 #endif
 
-/**
- *     lock_may_read - checks that the region is free of locks
- *     @inode: the inode that is being read
- *     @start: the first byte to read
- *     @len: the number of bytes to read
- *
- *     Emulates Windows locking requirements.  Whole-file
- *     mandatory locks (share modes) can prohibit a read and
- *     byte-range POSIX locks can prohibit a read if they overlap.
- *
- *     N.B. this function is only ever called
- *     from knfsd and ownership of locks is never checked.
- */
-int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
-{
-       struct file_lock *fl;
-       int result = 1;
-
-       spin_lock(&inode->i_lock);
-       for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
-               if (IS_POSIX(fl)) {
-                       if (fl->fl_type == F_RDLCK)
-                               continue;
-                       if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
-                               continue;
-               } else if (IS_FLOCK(fl)) {
-                       if (!(fl->fl_type & LOCK_MAND))
-                               continue;
-                       if (fl->fl_type & LOCK_READ)
-                               continue;
-               } else
-                       continue;
-               result = 0;
-               break;
-       }
-       spin_unlock(&inode->i_lock);
-       return result;
-}
-
-EXPORT_SYMBOL(lock_may_read);
-
-/**
- *     lock_may_write - checks that the region is free of locks
- *     @inode: the inode that is being written
- *     @start: the first byte to write
- *     @len: the number of bytes to write
- *
- *     Emulates Windows locking requirements.  Whole-file
- *     mandatory locks (share modes) can prohibit a write and
- *     byte-range POSIX locks can prohibit a write if they overlap.
- *
- *     N.B. this function is only ever called
- *     from knfsd and ownership of locks is never checked.
- */
-int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
-{
-       struct file_lock *fl;
-       int result = 1;
-
-       spin_lock(&inode->i_lock);
-       for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
-               if (IS_POSIX(fl)) {
-                       if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
-                               continue;
-               } else if (IS_FLOCK(fl)) {
-                       if (!(fl->fl_type & LOCK_MAND))
-                               continue;
-                       if (fl->fl_type & LOCK_WRITE)
-                               continue;
-               } else
-                       continue;
-               result = 0;
-               break;
-       }
-       spin_unlock(&inode->i_lock);
-       return result;
-}
-
-EXPORT_SYMBOL(lock_may_write);
-
 static int __init filelock_init(void)
 {
        int i;