BUG_ON() Conversion in mm/memory.c
[safe/jmp/linux-2.6] / fs / locks.c
index 3fa6a7c..56f996e 100644 (file)
 #include <linux/smp_lock.h>
 #include <linux/syscalls.h>
 #include <linux/time.h>
+#include <linux/rcupdate.h>
 
 #include <asm/semaphore.h>
 #include <asm/uaccess.h>
@@ -138,10 +139,7 @@ int lease_break_time = 45;
 #define for_each_lock(inode, lockp) \
        for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
 
-LIST_HEAD(file_lock_list);
-
-EXPORT_SYMBOL(file_lock_list);
-
+static LIST_HEAD(file_lock_list);
 static LIST_HEAD(blocked_list);
 
 static kmem_cache_t *filelock_cache;
@@ -152,8 +150,23 @@ static struct file_lock *locks_alloc_lock(void)
        return kmem_cache_alloc(filelock_cache, SLAB_KERNEL);
 }
 
+static void locks_release_private(struct file_lock *fl)
+{
+       if (fl->fl_ops) {
+               if (fl->fl_ops->fl_release_private)
+                       fl->fl_ops->fl_release_private(fl);
+               fl->fl_ops = NULL;
+       }
+       if (fl->fl_lmops) {
+               if (fl->fl_lmops->fl_release_private)
+                       fl->fl_lmops->fl_release_private(fl);
+               fl->fl_lmops = NULL;
+       }
+
+}
+
 /* Free a lock which is not in use. */
-static inline void locks_free_lock(struct file_lock *fl)
+static void locks_free_lock(struct file_lock *fl)
 {
        if (fl == NULL) {
                BUG();
@@ -168,18 +181,7 @@ static inline void locks_free_lock(struct file_lock *fl)
        if (!list_empty(&fl->fl_link))
                panic("Attempting to free lock on active lock list");
 
-       if (fl->fl_ops) {
-               if (fl->fl_ops->fl_release_private)
-                       fl->fl_ops->fl_release_private(fl);
-               fl->fl_ops = NULL;
-       }
-
-       if (fl->fl_lmops) {
-               if (fl->fl_lmops->fl_release_private)
-                       fl->fl_lmops->fl_release_private(fl);
-               fl->fl_lmops = NULL;
-       }
-
+       locks_release_private(fl);
        kmem_cache_free(filelock_cache, fl);
 }
 
@@ -217,24 +219,46 @@ static void init_once(void *foo, kmem_cache_t *cache, unsigned long flags)
        locks_init_lock(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) {
+               if (fl->fl_lmops->fl_copy_lock)
+                       fl->fl_lmops->fl_copy_lock(new, fl);
+               new->fl_lmops = fl->fl_lmops;
+       }
+}
+
 /*
  * Initialize a new lock from an existing file_lock structure.
  */
-void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
+static void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
 {
        new->fl_owner = fl->fl_owner;
        new->fl_pid = fl->fl_pid;
-       new->fl_file = fl->fl_file;
+       new->fl_file = NULL;
        new->fl_flags = fl->fl_flags;
        new->fl_type = fl->fl_type;
        new->fl_start = fl->fl_start;
        new->fl_end = fl->fl_end;
+       new->fl_ops = NULL;
+       new->fl_lmops = NULL;
+}
+
+void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
+{
+       locks_release_private(new);
+
+       __locks_copy_lock(new, fl);
+       new->fl_file = fl->fl_file;
        new->fl_ops = fl->fl_ops;
        new->fl_lmops = fl->fl_lmops;
-       if (fl->fl_ops && fl->fl_ops->fl_copy_lock)
-               fl->fl_ops->fl_copy_lock(new, fl);
-       if (fl->fl_lmops && fl->fl_lmops->fl_copy_lock)
-               fl->fl_lmops->fl_copy_lock(new, fl);
+
+       locks_copy_private(new, fl);
 }
 
 EXPORT_SYMBOL(locks_copy_lock);
@@ -315,21 +339,22 @@ static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
        /* POSIX-1996 leaves the case l->l_len < 0 undefined;
           POSIX-2001 defines it. */
        start += l->l_start;
-       end = start + l->l_len - 1;
-       if (l->l_len < 0) {
+       if (start < 0)
+               return -EINVAL;
+       fl->fl_end = OFFSET_MAX;
+       if (l->l_len > 0) {
+               end = start + l->l_len - 1;
+               fl->fl_end = end;
+       } else if (l->l_len < 0) {
                end = start - 1;
+               fl->fl_end = end;
                start += l->l_len;
+               if (start < 0)
+                       return -EINVAL;
        }
-
-       if (start < 0)
-               return -EINVAL;
-       if (l->l_len > 0 && end < 0)
-               return -EOVERFLOW;
-
        fl->fl_start = start;   /* we record the absolute position */
-       fl->fl_end = end;
-       if (l->l_len == 0)
-               fl->fl_end = OFFSET_MAX;
+       if (fl->fl_end < fl->fl_start)
+               return -EOVERFLOW;
        
        fl->fl_owner = current->files;
        fl->fl_pid = current->tgid;
@@ -361,14 +386,21 @@ static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
                return -EINVAL;
        }
 
-       if (((start += l->l_start) < 0) || (l->l_len < 0))
+       start += l->l_start;
+       if (start < 0)
                return -EINVAL;
-       fl->fl_end = start + l->l_len - 1;
-       if (l->l_len > 0 && fl->fl_end < 0)
-               return -EOVERFLOW;
+       fl->fl_end = OFFSET_MAX;
+       if (l->l_len > 0) {
+               fl->fl_end = start + l->l_len - 1;
+       } else if (l->l_len < 0) {
+               fl->fl_end = start - 1;
+               start += l->l_len;
+               if (start < 0)
+                       return -EINVAL;
+       }
        fl->fl_start = start;   /* we record the absolute position */
-       if (l->l_len == 0)
-               fl->fl_end = OFFSET_MAX;
+       if (fl->fl_end < fl->fl_start)
+               return -EOVERFLOW;
        
        fl->fl_owner = current->files;
        fl->fl_pid = current->tgid;
@@ -466,8 +498,7 @@ static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
 /*
  * Check whether two locks have the same owner.
  */
-static inline int
-posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
+static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
 {
        if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner)
                return fl2->fl_lmops == fl1->fl_lmops &&
@@ -478,7 +509,7 @@ posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
 /* Remove waiter from blocker's block list.
  * When blocker ends up pointing to itself then the list is empty.
  */
-static inline void __locks_delete_block(struct file_lock *waiter)
+static void __locks_delete_block(struct file_lock *waiter)
 {
        list_del_init(&waiter->fl_block);
        list_del_init(&waiter->fl_link);
@@ -646,8 +677,9 @@ static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *w
        return result;
 }
 
-struct file_lock *
-posix_test_lock(struct file *filp, struct file_lock *fl)
+int
+posix_test_lock(struct file *filp, struct file_lock *fl,
+               struct file_lock *conflock)
 {
        struct file_lock *cfl;
 
@@ -658,9 +690,13 @@ posix_test_lock(struct file *filp, struct file_lock *fl)
                if (posix_locks_conflict(cfl, fl))
                        break;
        }
+       if (cfl) {
+               __locks_copy_lock(conflock, cfl);
+               unlock_kernel();
+               return 1;
+       }
        unlock_kernel();
-
-       return (cfl);
+       return 0;
 }
 
 EXPORT_SYMBOL(posix_test_lock);
@@ -828,12 +864,16 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request)
                /* Detect adjacent or overlapping regions (if same lock type)
                 */
                if (request->fl_type == fl->fl_type) {
+                       /* In all comparisons of start vs end, use
+                        * "start - 1" rather than "end + 1". If end
+                        * is OFFSET_MAX, end + 1 will become negative.
+                        */
                        if (fl->fl_end < request->fl_start - 1)
                                goto next_lock;
                        /* If the next lock in the list has entirely bigger
                         * addresses than the new one, insert the lock here.
                         */
-                       if (fl->fl_start > request->fl_end + 1)
+                       if (fl->fl_start - 1 > request->fl_end)
                                break;
 
                        /* If we come here, the new and old lock are of the
@@ -892,7 +932,8 @@ static int __posix_lock_file(struct inode *inode, struct file_lock *request)
                                fl->fl_start = request->fl_start;
                                fl->fl_end = request->fl_end;
                                fl->fl_type = request->fl_type;
-                               fl->fl_u = request->fl_u;
+                               locks_release_private(fl);
+                               locks_copy_private(fl, request);
                                request = fl;
                                added = 1;
                        }
@@ -1092,7 +1133,6 @@ static void time_out_leases(struct inode *inode)
                        before = &fl->fl_next;
                        continue;
                }
-               printk(KERN_INFO "lease broken - owner pid = %d\n", fl->fl_pid);
                lease_modify(before, fl->fl_type & ~F_INPROGRESS);
                if (fl == *before)      /* lease_modify may have freed fl */
                        before = &fl->fl_next;
@@ -1276,7 +1316,7 @@ int fcntl_getlease(struct file *filp)
  */
 static int __setlease(struct file *filp, long arg, struct file_lock **flp)
 {
-       struct file_lock *fl, **before, **my_before = NULL, *lease = *flp;
+       struct file_lock *fl, **before, **my_before = NULL, *lease;
        struct dentry *dentry = filp->f_dentry;
        struct inode *inode = dentry->d_inode;
        int error, rdlease_count = 0, wrlease_count = 0;
@@ -1287,6 +1327,8 @@ static int __setlease(struct file *filp, long arg, struct file_lock **flp)
        if (!flp || !(*flp) || !(*flp)->fl_lmops || !(*flp)->fl_lmops->fl_break)
                goto out;
 
+       lease = *flp;
+
        error = -EAGAIN;
        if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
                goto out;
@@ -1415,7 +1457,7 @@ int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
        lock_kernel();
 
        error = __setlease(filp, arg, &flp);
-       if (error)
+       if (error || arg == F_UNLCK)
                goto out_unlock;
 
        error = fasync_helper(fd, filp, 1, &flp->fl_fasync);
@@ -1531,7 +1573,7 @@ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
  */
 int fcntl_getlk(struct file *filp, struct flock __user *l)
 {
-       struct file_lock *fl, file_lock;
+       struct file_lock *fl, cfl, file_lock;
        struct flock flock;
        int error;
 
@@ -1548,12 +1590,14 @@ int fcntl_getlk(struct file *filp, struct flock __user *l)
 
        if (filp->f_op && filp->f_op->lock) {
                error = filp->f_op->lock(filp, F_GETLK, &file_lock);
+               if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
+                       file_lock.fl_ops->fl_release_private(&file_lock);
                if (error < 0)
                        goto out;
                else
                  fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
        } else {
-               fl = posix_test_lock(filp, &file_lock);
+               fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL);
        }
  
        flock.l_type = F_UNLCK;
@@ -1587,7 +1631,8 @@ out:
 /* Apply the lock described by l to an open file descriptor.
  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  */
-int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock __user *l)
+int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
+               struct flock __user *l)
 {
        struct file_lock *file_lock = locks_alloc_lock();
        struct flock flock;
@@ -1616,6 +1661,7 @@ int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock __user *l)
                goto out;
        }
 
+again:
        error = flock_to_posix_lock(filp, file_lock, &flock);
        if (error)
                goto out;
@@ -1644,25 +1690,33 @@ int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock __user *l)
        if (error)
                goto out;
 
-       if (filp->f_op && filp->f_op->lock != NULL) {
+       if (filp->f_op && filp->f_op->lock != NULL)
                error = filp->f_op->lock(filp, cmd, file_lock);
-               goto out;
-       }
+       else {
+               for (;;) {
+                       error = __posix_lock_file(inode, file_lock);
+                       if ((error != -EAGAIN) || (cmd == F_SETLK))
+                               break;
+                       error = wait_event_interruptible(file_lock->fl_wait,
+                                       !file_lock->fl_next);
+                       if (!error)
+                               continue;
 
-       for (;;) {
-               error = __posix_lock_file(inode, file_lock);
-               if ((error != -EAGAIN) || (cmd == F_SETLK))
+                       locks_delete_block(file_lock);
                        break;
-               error = wait_event_interruptible(file_lock->fl_wait,
-                               !file_lock->fl_next);
-               if (!error)
-                       continue;
+               }
+       }
 
-               locks_delete_block(file_lock);
-               break;
+       /*
+        * Attempt to detect a close/fcntl race and recover by
+        * releasing the lock that was just acquired.
+        */
+       if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) {
+               flock.l_type = F_UNLCK;
+               goto again;
        }
 
- out:
+out:
        locks_free_lock(file_lock);
        return error;
 }
@@ -1673,7 +1727,7 @@ int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock __user *l)
  */
 int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
 {
-       struct file_lock *fl, file_lock;
+       struct file_lock *fl, cfl, file_lock;
        struct flock64 flock;
        int error;
 
@@ -1690,12 +1744,14 @@ int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
 
        if (filp->f_op && filp->f_op->lock) {
                error = filp->f_op->lock(filp, F_GETLK, &file_lock);
+               if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
+                       file_lock.fl_ops->fl_release_private(&file_lock);
                if (error < 0)
                        goto out;
                else
                  fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
        } else {
-               fl = posix_test_lock(filp, &file_lock);
+               fl = (posix_test_lock(filp, &file_lock, &cfl) ? &cfl : NULL);
        }
  
        flock.l_type = F_UNLCK;
@@ -1718,7 +1774,8 @@ out:
 /* Apply the lock described by l to an open file descriptor.
  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  */
-int fcntl_setlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
+int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
+               struct flock64 __user *l)
 {
        struct file_lock *file_lock = locks_alloc_lock();
        struct flock64 flock;
@@ -1747,6 +1804,7 @@ int fcntl_setlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
                goto out;
        }
 
+again:
        error = flock64_to_posix_lock(filp, file_lock, &flock);
        if (error)
                goto out;
@@ -1775,22 +1833,30 @@ int fcntl_setlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
        if (error)
                goto out;
 
-       if (filp->f_op && filp->f_op->lock != NULL) {
+       if (filp->f_op && filp->f_op->lock != NULL)
                error = filp->f_op->lock(filp, cmd, file_lock);
-               goto out;
-       }
+       else {
+               for (;;) {
+                       error = __posix_lock_file(inode, file_lock);
+                       if ((error != -EAGAIN) || (cmd == F_SETLK64))
+                               break;
+                       error = wait_event_interruptible(file_lock->fl_wait,
+                                       !file_lock->fl_next);
+                       if (!error)
+                               continue;
 
-       for (;;) {
-               error = __posix_lock_file(inode, file_lock);
-               if ((error != -EAGAIN) || (cmd == F_SETLK64))
+                       locks_delete_block(file_lock);
                        break;
-               error = wait_event_interruptible(file_lock->fl_wait,
-                               !file_lock->fl_next);
-               if (!error)
-                       continue;
+               }
+       }
 
-               locks_delete_block(file_lock);
-               break;
+       /*
+        * Attempt to detect a close/fcntl race and recover by
+        * releasing the lock that was just acquired.
+        */
+       if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) {
+               flock.l_type = F_UNLCK;
+               goto again;
        }
 
 out:
@@ -1873,6 +1939,8 @@ void locks_remove_flock(struct file *filp)
                        .fl_end = OFFSET_MAX,
                };
                filp->f_op->flock(filp, F_SETLKW, &fl);
+               if (fl.fl_ops && fl.fl_ops->fl_release_private)
+                       fl.fl_ops->fl_release_private(&fl);
        }
 
        lock_kernel();
@@ -1880,12 +1948,7 @@ void locks_remove_flock(struct file *filp)
 
        while ((fl = *before) != NULL) {
                if (fl->fl_file == filp) {
-                       /*
-                        * We might have a POSIX lock that was created at the same time
-                        * the filp was closed for the last time. Just remove that too,
-                        * regardless of ownership, since nobody can own it.
-                        */
-                       if (IS_FLOCK(fl) || IS_POSIX(fl)) {
+                       if (IS_FLOCK(fl)) {
                                locks_delete_lock(before);
                                continue;
                        }
@@ -1902,43 +1965,24 @@ void locks_remove_flock(struct file *filp)
 }
 
 /**
- *     posix_block_lock - blocks waiting for a file lock
- *     @blocker: the lock which is blocking
- *     @waiter: the lock which conflicts and has to wait
- *
- * lockd needs to block waiting for locks.
- */
-void
-posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
-{
-       locks_insert_block(blocker, waiter);
-}
-
-EXPORT_SYMBOL(posix_block_lock);
-
-/**
  *     posix_unblock_lock - stop waiting for a file lock
  *      @filp:   how the file was opened
  *     @waiter: the lock which was waiting
  *
  *     lockd needs to block waiting for locks.
  */
-void
+int
 posix_unblock_lock(struct file *filp, struct file_lock *waiter)
 {
-       /* 
-        * A remote machine may cancel the lock request after it's been
-        * granted locally.  If that happens, we need to delete the lock.
-        */
+       int status = 0;
+
        lock_kernel();
-       if (waiter->fl_next) {
+       if (waiter->fl_next)
                __locks_delete_block(waiter);
-               unlock_kernel();
-       } else {
-               unlock_kernel();
-               waiter->fl_type = F_UNLCK;
-               posix_lock_file(filp, waiter);
-       }
+       else
+               status = -ENOENT;
+       unlock_kernel();
+       return status;
 }
 
 EXPORT_SYMBOL(posix_unblock_lock);
@@ -2175,21 +2219,24 @@ void steal_locks(fl_owner_t from)
 {
        struct files_struct *files = current->files;
        int i, j;
+       struct fdtable *fdt;
 
        if (from == files)
                return;
 
        lock_kernel();
        j = 0;
+       rcu_read_lock();
+       fdt = files_fdtable(files);
        for (;;) {
                unsigned long set;
                i = j * __NFDBITS;
-               if (i >= files->max_fdset || i >= files->max_fds)
+               if (i >= fdt->max_fdset || i >= fdt->max_fds)
                        break;
-               set = files->open_fds->fds_bits[j++];
+               set = fdt->open_fds->fds_bits[j++];
                while (set) {
                        if (set & 1) {
-                               struct file *file = files->fd[i];
+                               struct file *file = fdt->fd[i];
                                if (file)
                                        __steal_locks(file, from);
                        }
@@ -2197,6 +2244,7 @@ void steal_locks(fl_owner_t from)
                        set >>= 1;
                }
        }
+       rcu_read_unlock();
        unlock_kernel();
 }
 EXPORT_SYMBOL(steal_locks);