Btrfs: correct error-handling zlib error handling
[safe/jmp/linux-2.6] / fs / eventfd.c
index 480e2b3..2a701d5 100644 (file)
@@ -15,9 +15,9 @@
 #include <linux/spinlock.h>
 #include <linux/anon_inodes.h>
 #include <linux/eventfd.h>
+#include <linux/syscalls.h>
 
 struct eventfd_ctx {
-       spinlock_t lock;
        wait_queue_head_t wqh;
        /*
         * Every time that a write(2) is performed on an eventfd, the
@@ -28,6 +28,7 @@ struct eventfd_ctx {
         * issue a wakeup.
         */
        __u64 count;
+       unsigned int flags;
 };
 
 /*
@@ -45,13 +46,13 @@ int eventfd_signal(struct file *file, int n)
 
        if (n < 0)
                return -EINVAL;
-       spin_lock_irqsave(&ctx->lock, flags);
+       spin_lock_irqsave(&ctx->wqh.lock, flags);
        if (ULLONG_MAX - ctx->count < n)
                n = (int) (ULLONG_MAX - ctx->count);
        ctx->count += n;
        if (waitqueue_active(&ctx->wqh))
-               wake_up_locked(&ctx->wqh);
-       spin_unlock_irqrestore(&ctx->lock, flags);
+               wake_up_locked_poll(&ctx->wqh, POLLIN);
+       spin_unlock_irqrestore(&ctx->wqh.lock, flags);
 
        return n;
 }
@@ -70,14 +71,14 @@ static unsigned int eventfd_poll(struct file *file, poll_table *wait)
 
        poll_wait(file, &ctx->wqh, wait);
 
-       spin_lock_irqsave(&ctx->lock, flags);
+       spin_lock_irqsave(&ctx->wqh.lock, flags);
        if (ctx->count > 0)
                events |= POLLIN;
        if (ctx->count == ULLONG_MAX)
                events |= POLLERR;
        if (ULLONG_MAX - 1 > ctx->count)
                events |= POLLOUT;
-       spin_unlock_irqrestore(&ctx->lock, flags);
+       spin_unlock_irqrestore(&ctx->wqh.lock, flags);
 
        return events;
 }
@@ -87,22 +88,20 @@ static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
 {
        struct eventfd_ctx *ctx = file->private_data;
        ssize_t res;
-       __u64 ucnt;
+       __u64 ucnt = 0;
        DECLARE_WAITQUEUE(wait, current);
 
        if (count < sizeof(ucnt))
                return -EINVAL;
-       spin_lock_irq(&ctx->lock);
+       spin_lock_irq(&ctx->wqh.lock);
        res = -EAGAIN;
-       ucnt = ctx->count;
-       if (ucnt > 0)
+       if (ctx->count > 0)
                res = sizeof(ucnt);
        else if (!(file->f_flags & O_NONBLOCK)) {
                __add_wait_queue(&ctx->wqh, &wait);
                for (res = 0;;) {
                        set_current_state(TASK_INTERRUPTIBLE);
                        if (ctx->count > 0) {
-                               ucnt = ctx->count;
                                res = sizeof(ucnt);
                                break;
                        }
@@ -110,19 +109,20 @@ static ssize_t eventfd_read(struct file *file, char __user *buf, size_t count,
                                res = -ERESTARTSYS;
                                break;
                        }
-                       spin_unlock_irq(&ctx->lock);
+                       spin_unlock_irq(&ctx->wqh.lock);
                        schedule();
-                       spin_lock_irq(&ctx->lock);
+                       spin_lock_irq(&ctx->wqh.lock);
                }
                __remove_wait_queue(&ctx->wqh, &wait);
                __set_current_state(TASK_RUNNING);
        }
-       if (res > 0) {
-               ctx->count = 0;
+       if (likely(res > 0)) {
+               ucnt = (ctx->flags & EFD_SEMAPHORE) ? 1 : ctx->count;
+               ctx->count -= ucnt;
                if (waitqueue_active(&ctx->wqh))
-                       wake_up_locked(&ctx->wqh);
+                       wake_up_locked_poll(&ctx->wqh, POLLOUT);
        }
-       spin_unlock_irq(&ctx->lock);
+       spin_unlock_irq(&ctx->wqh.lock);
        if (res > 0 && put_user(ucnt, (__u64 __user *) buf))
                return -EFAULT;
 
@@ -143,7 +143,7 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c
                return -EFAULT;
        if (ucnt == ULLONG_MAX)
                return -EINVAL;
-       spin_lock_irq(&ctx->lock);
+       spin_lock_irq(&ctx->wqh.lock);
        res = -EAGAIN;
        if (ULLONG_MAX - ctx->count > ucnt)
                res = sizeof(ucnt);
@@ -159,19 +159,19 @@ static ssize_t eventfd_write(struct file *file, const char __user *buf, size_t c
                                res = -ERESTARTSYS;
                                break;
                        }
-                       spin_unlock_irq(&ctx->lock);
+                       spin_unlock_irq(&ctx->wqh.lock);
                        schedule();
-                       spin_lock_irq(&ctx->lock);
+                       spin_lock_irq(&ctx->wqh.lock);
                }
                __remove_wait_queue(&ctx->wqh, &wait);
                __set_current_state(TASK_RUNNING);
        }
-       if (res > 0) {
+       if (likely(res > 0)) {
                ctx->count += ucnt;
                if (waitqueue_active(&ctx->wqh))
-                       wake_up_locked(&ctx->wqh);
+                       wake_up_locked_poll(&ctx->wqh, POLLIN);
        }
-       spin_unlock_irq(&ctx->lock);
+       spin_unlock_irq(&ctx->wqh.lock);
 
        return res;
 }
@@ -198,31 +198,39 @@ struct file *eventfd_fget(int fd)
        return file;
 }
 
-asmlinkage long sys_eventfd(unsigned int count)
+SYSCALL_DEFINE2(eventfd2, unsigned int, count, int, flags)
 {
-       int error, fd;
+       int fd;
        struct eventfd_ctx *ctx;
-       struct file *file;
-       struct inode *inode;
+
+       /* Check the EFD_* constants for consistency.  */
+       BUILD_BUG_ON(EFD_CLOEXEC != O_CLOEXEC);
+       BUILD_BUG_ON(EFD_NONBLOCK != O_NONBLOCK);
+
+       if (flags & ~EFD_FLAGS_SET)
+               return -EINVAL;
 
        ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
        if (!ctx)
                return -ENOMEM;
 
        init_waitqueue_head(&ctx->wqh);
-       spin_lock_init(&ctx->lock);
        ctx->count = count;
+       ctx->flags = flags;
 
        /*
         * When we call this, the initialization must be complete, since
         * anon_inode_getfd() will install the fd.
         */
-       error = anon_inode_getfd(&fd, &inode, &file, "[eventfd]",
-                                &eventfd_fops, ctx);
-       if (!error)
-               return fd;
+       fd = anon_inode_getfd("[eventfd]", &eventfd_fops, ctx,
+                             flags & EFD_SHARED_FCNTL_FLAGS);
+       if (fd < 0)
+               kfree(ctx);
+       return fd;
+}
 
-       kfree(ctx);
-       return error;
+SYSCALL_DEFINE1(eventfd, unsigned int, count)
+{
+       return sys_eventfd2(count, 0);
 }