Merge branch 'flock' of git://git.kernel.org/pub/scm/linux/kernel/git/arnd/bkl
[sfrench/cifs-2.6.git] / fs / locks.c
index ab24d49fc04844e93bbedb1cd7c61083cb74e2b8..50ec15927aab3164a456875e6c11b1f4fa2f8bef 100644 (file)
@@ -142,14 +142,32 @@ int lease_break_time = 45;
 
 static LIST_HEAD(file_lock_list);
 static LIST_HEAD(blocked_list);
+static DEFINE_SPINLOCK(file_lock_lock);
+
+/*
+ * Protects the two list heads above, plus the inode->i_flock list
+ * FIXME: should use a spinlock, once lockd and ceph are ready.
+ */
+void lock_flocks(void)
+{
+       spin_lock(&file_lock_lock);
+}
+EXPORT_SYMBOL_GPL(lock_flocks);
+
+void unlock_flocks(void)
+{
+       spin_unlock(&file_lock_lock);
+}
+EXPORT_SYMBOL_GPL(unlock_flocks);
 
 static struct kmem_cache *filelock_cache __read_mostly;
 
 /* Allocate an empty lock structure. */
-static struct file_lock *locks_alloc_lock(void)
+struct file_lock *locks_alloc_lock(void)
 {
        return kmem_cache_alloc(filelock_cache, GFP_KERNEL);
 }
+EXPORT_SYMBOL_GPL(locks_alloc_lock);
 
 void locks_release_private(struct file_lock *fl)
 {
@@ -511,9 +529,9 @@ static void __locks_delete_block(struct file_lock *waiter)
  */
 static void locks_delete_block(struct file_lock *waiter)
 {
-       lock_kernel();
+       lock_flocks();
        __locks_delete_block(waiter);
-       unlock_kernel();
+       unlock_flocks();
 }
 
 /* Insert waiter into blocker's block list.
@@ -644,7 +662,7 @@ posix_test_lock(struct file *filp, struct file_lock *fl)
 {
        struct file_lock *cfl;
 
-       lock_kernel();
+       lock_flocks();
        for (cfl = filp->f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
                if (!IS_POSIX(cfl))
                        continue;
@@ -657,7 +675,7 @@ posix_test_lock(struct file *filp, struct file_lock *fl)
                        fl->fl_pid = pid_vnr(cfl->fl_nspid);
        } else
                fl->fl_type = F_UNLCK;
-       unlock_kernel();
+       unlock_flocks();
        return;
 }
 EXPORT_SYMBOL(posix_test_lock);
@@ -730,18 +748,16 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
        int error = 0;
        int found = 0;
 
-       lock_kernel();
-       if (request->fl_flags & FL_ACCESS)
-               goto find_conflict;
-
-       if (request->fl_type != F_UNLCK) {
-               error = -ENOMEM;
+       if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
                new_fl = locks_alloc_lock();
-               if (new_fl == NULL)
-                       goto out;
-               error = 0;
+               if (!new_fl)
+                       return -ENOMEM;
        }
 
+       lock_flocks();
+       if (request->fl_flags & FL_ACCESS)
+               goto find_conflict;
+
        for_each_lock(inode, before) {
                struct file_lock *fl = *before;
                if (IS_POSIX(fl))
@@ -767,8 +783,11 @@ static int flock_lock_file(struct file *filp, struct file_lock *request)
         * If a higher-priority process was blocked on the old file lock,
         * give it the opportunity to lock the file.
         */
-       if (found)
+       if (found) {
+               unlock_flocks();
                cond_resched();
+               lock_flocks();
+       }
 
 find_conflict:
        for_each_lock(inode, before) {
@@ -794,7 +813,7 @@ find_conflict:
        error = 0;
 
 out:
-       unlock_kernel();
+       unlock_flocks();
        if (new_fl)
                locks_free_lock(new_fl);
        return error;
@@ -823,7 +842,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
                new_fl2 = locks_alloc_lock();
        }
 
-       lock_kernel();
+       lock_flocks();
        if (request->fl_type != F_UNLCK) {
                for_each_lock(inode, before) {
                        fl = *before;
@@ -991,7 +1010,7 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request, str
                locks_wake_up_blocks(left);
        }
  out:
-       unlock_kernel();
+       unlock_flocks();
        /*
         * Free any unused locks.
         */
@@ -1066,14 +1085,14 @@ int locks_mandatory_locked(struct inode *inode)
        /*
         * Search the lock list for this inode for any POSIX locks.
         */
-       lock_kernel();
+       lock_flocks();
        for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
                if (!IS_POSIX(fl))
                        continue;
                if (fl->fl_owner != owner)
                        break;
        }
-       unlock_kernel();
+       unlock_flocks();
        return fl ? -EAGAIN : 0;
 }
 
@@ -1186,7 +1205,7 @@ int __break_lease(struct inode *inode, unsigned int mode)
 
        new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
 
-       lock_kernel();
+       lock_flocks();
 
        time_out_leases(inode);
 
@@ -1247,8 +1266,10 @@ restart:
                        break_time++;
        }
        locks_insert_block(flock, new_fl);
+       unlock_flocks();
        error = wait_event_interruptible_timeout(new_fl->fl_wait,
                                                !new_fl->fl_next, break_time);
+       lock_flocks();
        __locks_delete_block(new_fl);
        if (error >= 0) {
                if (error == 0)
@@ -1263,7 +1284,7 @@ restart:
        }
 
 out:
-       unlock_kernel();
+       unlock_flocks();
        if (!IS_ERR(new_fl))
                locks_free_lock(new_fl);
        return error;
@@ -1319,7 +1340,7 @@ int fcntl_getlease(struct file *filp)
        struct file_lock *fl;
        int type = F_UNLCK;
 
-       lock_kernel();
+       lock_flocks();
        time_out_leases(filp->f_path.dentry->d_inode);
        for (fl = filp->f_path.dentry->d_inode->i_flock; fl && IS_LEASE(fl);
                        fl = fl->fl_next) {
@@ -1328,7 +1349,7 @@ int fcntl_getlease(struct file *filp)
                        break;
                }
        }
-       unlock_kernel();
+       unlock_flocks();
        return type;
 }
 
@@ -1341,12 +1362,11 @@ int fcntl_getlease(struct file *filp)
  *     The (input) flp->fl_lmops->fl_break function is required
  *     by break_lease().
  *
- *     Called with kernel lock held.
+ *     Called with file_lock_lock held.
  */
 int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
 {
        struct file_lock *fl, **before, **my_before = NULL, *lease;
-       struct file_lock *new_fl = NULL;
        struct dentry *dentry = filp->f_path.dentry;
        struct inode *inode = dentry->d_inode;
        int error, rdlease_count = 0, wrlease_count = 0;
@@ -1366,11 +1386,6 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
        lease = *flp;
 
        if (arg != F_UNLCK) {
-               error = -ENOMEM;
-               new_fl = locks_alloc_lock();
-               if (new_fl == NULL)
-                       goto out;
-
                error = -EAGAIN;
                if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
                        goto out;
@@ -1415,7 +1430,6 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
                goto out;
        }
 
-       error = 0;
        if (arg == F_UNLCK)
                goto out;
 
@@ -1423,20 +1437,24 @@ int generic_setlease(struct file *filp, long arg, struct file_lock **flp)
        if (!leases_enable)
                goto out;
 
-       locks_copy_lock(new_fl, lease);
-       locks_insert_lock(before, new_fl);
-
-       *flp = new_fl;
+       locks_insert_lock(before, lease);
        return 0;
 
 out:
-       if (new_fl != NULL)
-               locks_free_lock(new_fl);
+       locks_free_lock(lease);
        return error;
 }
 EXPORT_SYMBOL(generic_setlease);
 
- /**
+static int __vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
+{
+       if (filp->f_op && filp->f_op->setlease)
+               return filp->f_op->setlease(filp, arg, lease);
+       else
+               return generic_setlease(filp, arg, lease);
+}
+
+/**
  *     vfs_setlease        -       sets a lease on an open file
  *     @filp: file pointer
  *     @arg: type of lease to obtain
@@ -1467,12 +1485,9 @@ int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
 {
        int error;
 
-       lock_kernel();
-       if (filp->f_op && filp->f_op->setlease)
-               error = filp->f_op->setlease(filp, arg, lease);
-       else
-               error = generic_setlease(filp, arg, lease);
-       unlock_kernel();
+       lock_flocks();
+       error = __vfs_setlease(filp, arg, lease);
+       unlock_flocks();
 
        return error;
 }
@@ -1490,33 +1505,47 @@ EXPORT_SYMBOL_GPL(vfs_setlease);
  */
 int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
 {
-       struct file_lock fl, *flp = &fl;
+       struct file_lock *fl;
+       struct fasync_struct *new;
        struct inode *inode = filp->f_path.dentry->d_inode;
        int error;
 
-       locks_init_lock(&fl);
-       error = lease_init(filp, arg, &fl);
-       if (error)
-               return error;
+       fl = lease_alloc(filp, arg);
+       if (IS_ERR(fl))
+               return PTR_ERR(fl);
 
-       lock_kernel();
-
-       error = vfs_setlease(filp, arg, &flp);
+       new = fasync_alloc();
+       if (!new) {
+               locks_free_lock(fl);
+               return -ENOMEM;
+       }
+       lock_flocks();
+       error = __vfs_setlease(filp, arg, &fl);
        if (error || arg == F_UNLCK)
                goto out_unlock;
 
-       error = fasync_helper(fd, filp, 1, &flp->fl_fasync);
+       /*
+        * 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, &fl->fl_fasync, new))
+               new = NULL;
+
        if (error < 0) {
                /* remove lease just inserted by setlease */
-               flp->fl_type = F_UNLCK | F_INPROGRESS;
-               flp->fl_break_time = jiffies - 10;
+               fl->fl_type = F_UNLCK | F_INPROGRESS;
+               fl->fl_break_time = jiffies - 10;
                time_out_leases(inode);
                goto out_unlock;
        }
 
        error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
 out_unlock:
-       unlock_kernel();
+       unlock_flocks();
+       if (new)
+               fasync_free(new);
        return error;
 }
 
@@ -2020,7 +2049,7 @@ void locks_remove_flock(struct file *filp)
                        fl.fl_ops->fl_release_private(&fl);
        }
 
-       lock_kernel();
+       lock_flocks();
        before = &inode->i_flock;
 
        while ((fl = *before) != NULL) {
@@ -2038,7 +2067,7 @@ void locks_remove_flock(struct file *filp)
                }
                before = &fl->fl_next;
        }
-       unlock_kernel();
+       unlock_flocks();
 }
 
 /**
@@ -2053,12 +2082,12 @@ posix_unblock_lock(struct file *filp, struct file_lock *waiter)
 {
        int status = 0;
 
-       lock_kernel();
+       lock_flocks();
        if (waiter->fl_next)
                __locks_delete_block(waiter);
        else
                status = -ENOENT;
-       unlock_kernel();
+       unlock_flocks();
        return status;
 }
 
@@ -2085,7 +2114,7 @@ EXPORT_SYMBOL_GPL(vfs_cancel_lock);
 #include <linux/seq_file.h>
 
 static void lock_get_status(struct seq_file *f, struct file_lock *fl,
-                                                       int id, char *pfx)
+                           loff_t id, char *pfx)
 {
        struct inode *inode = NULL;
        unsigned int fl_pid;
@@ -2098,7 +2127,7 @@ static void lock_get_status(struct seq_file *f, struct file_lock *fl,
        if (fl->fl_file != NULL)
                inode = fl->fl_file->f_path.dentry->d_inode;
 
-       seq_printf(f, "%d:%s ", id, pfx);
+       seq_printf(f, "%lld:%s ", id, pfx);
        if (IS_POSIX(fl)) {
                seq_printf(f, "%6s %s ",
                             (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
@@ -2161,30 +2190,33 @@ static int locks_show(struct seq_file *f, void *v)
 
        fl = list_entry(v, struct file_lock, fl_link);
 
-       lock_get_status(f, fl, (long)f->private, "");
+       lock_get_status(f, fl, *((loff_t *)f->private), "");
 
        list_for_each_entry(bfl, &fl->fl_block, fl_block)
-               lock_get_status(f, bfl, (long)f->private, " ->");
+               lock_get_status(f, bfl, *((loff_t *)f->private), " ->");
 
-       f->private++;
        return 0;
 }
 
 static void *locks_start(struct seq_file *f, loff_t *pos)
 {
-       lock_kernel();
-       f->private = (void *)1;
+       loff_t *p = f->private;
+
+       lock_flocks();
+       *p = (*pos + 1);
        return seq_list_start(&file_lock_list, *pos);
 }
 
 static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
 {
+       loff_t *p = f->private;
+       ++*p;
        return seq_list_next(v, &file_lock_list, pos);
 }
 
 static void locks_stop(struct seq_file *f, void *v)
 {
-       unlock_kernel();
+       unlock_flocks();
 }
 
 static const struct seq_operations locks_seq_operations = {
@@ -2196,14 +2228,14 @@ static const struct seq_operations locks_seq_operations = {
 
 static int locks_open(struct inode *inode, struct file *filp)
 {
-       return seq_open(filp, &locks_seq_operations);
+       return seq_open_private(filp, &locks_seq_operations, sizeof(loff_t));
 }
 
 static const struct file_operations proc_locks_operations = {
        .open           = locks_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
-       .release        = seq_release,
+       .release        = seq_release_private,
 };
 
 static int __init proc_locks_init(void)
@@ -2231,7 +2263,7 @@ int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
 {
        struct file_lock *fl;
        int result = 1;
-       lock_kernel();
+       lock_flocks();
        for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
                if (IS_POSIX(fl)) {
                        if (fl->fl_type == F_RDLCK)
@@ -2248,7 +2280,7 @@ int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
                result = 0;
                break;
        }
-       unlock_kernel();
+       unlock_flocks();
        return result;
 }
 
@@ -2271,7 +2303,7 @@ int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
 {
        struct file_lock *fl;
        int result = 1;
-       lock_kernel();
+       lock_flocks();
        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)))
@@ -2286,7 +2318,7 @@ int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
                result = 0;
                break;
        }
-       unlock_kernel();
+       unlock_flocks();
        return result;
 }